In this article we
will discuss with you the program for checking whether given no. is prime or
not using console application in c#.net:
Step1: Take a new console application using c# as
a language in the Visual studio.
Step2: Read an input from the User in this
Variable “int_Number” for storing that number which you want to test for prime number.
Step3: Now if that input
number is 1 or 2 then it will be a prime number otherwise we have to loop from
2 till that number and Start dividing that input number from 2 till that number,
If it’s divisible by any of the number before the “input number-1” then it
means that it’s not a prime number otherwise it is a prime number.
using System;              
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrimeNo
{
    class Program
    {
        static void Main()
        {
            int int_Number;
            int
i;
            Console.WriteLine("Enter a number to check whether it is prime or
not?");
            int_Number = int.Parse(Console.ReadLine());
            if
((int_Number == 2) || (int_Number == 1))
            {
                Console.WriteLine("The Given number is prime number");
                Console.ReadLine();
            }
            else
            {
                for
(i = 2; i < int_Number; i++)
                {
                    if
(int_Number % i == 0)
                    {
                        Console.WriteLine("The Given number
is not prime number");
                        Console.ReadLine();
                        break;
                    }
                }
                if
(int_Number == i)
                {
                    Console.WriteLine("The Given number is prime number");
                    Console.ReadLine();
                }
            }
       }
    }
}
Output:-

 
No comments:
Post a Comment