Programming Course in C# ¡Free!

Prime number

 Friday, April 05, 2013 published by Exercises C#
Proposed exercise

Write a program which asks the user for an integer number and answers if it is a prime number or not.


Output



Solution


using System;
public class Prime
{
public static void Main()
{
int number, divider=2;

Console.Write("Enter a number: ");
number = Convert.ToInt32(Console.ReadLine());

while( number % divider != 0)
divider++;

if (divider == number)
Console.WriteLine("Is prime!!!");
else    
Console.WriteLine("Is not prime!!!");
}
}