Prime Number Program in C#
Advertisements
Prime Number Program in C#
Prime number is only divisible by 1 and number it self. Here we write code for this in C#. To display any message on screen we use WriteLine() the static method of Console class.
Prime Number Program in C#
using System; namespace example { class Prime_Number { public static void Main() { Console.Write("Enter a Number : "); int num; num = Convert.ToInt32(Console.ReadLine()); int k; k = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) { k++; } } if (k == 2) { Console.WriteLine("Entered Number is a Prime Number and the Largest Factor is {0}",num); } else { Console.WriteLine("Not a Prime Number"); } Console.ReadLine(); } } }
Output
Enter a Number : 23 Entered Number is a Prime Number and the Largest Factor is 23
Google Advertisment