2.32 - Prime number

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

Solution
using System;
public class Prime
{
    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");
    }
}

Popular Posts