Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Aravind GovindarajRank #430 · 5.8K reputationPosted Oct 21, 2022, 6:25 AM EDT
Hi Naresh, Please follow the below link
www.javatpoint.com/prime-number-program-in-csharp
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 18, 2022, 12:31 PM EDT
You can check the no. Is prime or not using below code in c#.
using System; namespace Demo { class MyApp { public static void Main() { int n = 5, a = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { a++; } } if (a == 2) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("Not a Prime Number"); } Console.ReadLine(); } } } Output 5 is a Prime NumberRajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 18, 2022, 1:57 PM EDT
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PrimeNumber { class Program { static void Main(string[] args) { int num; int count = 0; Console.Write("Enter a number to check if number is prime or not :"); num = int.Parse(Console.ReadLine()); if(num <= 1) { Console.WriteLine("\nInvalid number please Try Again"); } for(int i = 2; i < num/2; i++) { if(num % i == 0) { count = 1; break; } } if(count==1) { Console.WriteLine("\nEntered Number is Not Prime"); } else { Console.WriteLine("\nEntered Number is Prime"); } Console.ReadLine(); } } }