Prime number Learn programming C# Sharp

Lesson:

Flow Control


Exercise:

Prime number 51


Objetive:

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


Code:

using System;
public class exercise46
{
    public static void Main()
    {
        int number;
        int dividers = 0;

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

        for (int i = 1; i <= number; i++)
            if (number % i == 0)
                dividers++;

        if (dividers == 2)
            Console.WriteLine("The number is prime");
        else
            Console.WriteLine("The number is not prime");
    }
}