Example to check whether an integer (entered by the user) is a prime number or not using for loop and if…else statement.

A positive integer which is only divisible by 1 and itself is known as prime number.

**For example:** 17 is a prime number because it is only divisible by 1 and 17 but, 10 is not prime number because it is divisible by 1, 2, 5 and 10.

**C# Code:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Program { static void Main(string[] args) { int number, i; bool isPrime = true; string output; Console.Write("Enter a positive number :"); number = Convert.ToInt32(Console.ReadLine()); for (i = 2; i <= number / 2; ++i) { if (number % i == 0) { isPrime = false; break; } } if (isPrime) output = "This is a prime number"; else output = "This is not a prime number"; Console.WriteLine(output); Console.ReadLine(); } } |

**Output:**