Program to Check a Number is Prime Number

A number is said to be prime number if that number is greater than one and can only be divided by 1 or itself. For example, 2, 3, 5 and 7 etc are prime numbers. Here in this blog, we will see how to write a program in C# to check whether a number is prime number or not.

Program to Check a Number is Prime Number

Getting Started

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number.

For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 4 is composite because it is a product (2 × 2) in which both numbers are smaller than 4.

Primes are central in number theory because of the fundamental theorem of arithmetic: every natural number greater than 1 is either a prime itself or can be factorized as a product of primes that is unique up to their order

Find Prime Number From The Given Number

The below code example is written in the console application which describes how to check a given number is a prime number or not.

 static void Main(string[] args)  
 {  
   Console.Write("Enter a Number : ");  
   int number = int.Parse(Console.ReadLine());  
   bool IsPrime = true;  
   for (int i = 2; i < number / 2; i++)  
   {  
     if (number % i == 0)  
     {  
       IsPrime = false;  
       break;  
     }  
   }  
   if (IsPrime)  
   {  
     Console.Write("{0} is a Prime Number.",number);  
   }  
   else  
   {  
     Console.Write("{0} is not a Prime Number.",number);  
   }  
   Console.ReadKey();  
 }  

Prime Number

Output-1
 Enter Number: 87  
 87 is not a Prime Number  
Output-2
 Enter Number: 57  
 57 is a Prime Number  

In the above code snippet first, it assigns the entered number to a variable then loops using for loop Clouse, the loop starts from number two and will iterate till half of the entered number. During loop it also checks the number for prime number, If the entered number is divided by any of the iterator value, then the number is a prime number otherwise is not a prime number

Find the Prime Number from the given Interval

The following code example takes two numbers a argument and find the prime numbers available between the two numbers.

 static void Main(string[] args)  
 {  
   Console.Write("Enter the start number: ");  
   int number1 = int.Parse(Console.ReadLine());  
   Console.Write("Enter the end number: ");  
   int number2 = int.Parse(Console.ReadLine());  
   Console.WriteLine("The prime number/s of given range");  
   for (int i = number1; i <= number2; i++)  
   {  
     int counter = 0;  
     for (int j = 2; j <= i / 2; j++)  
     {  
       if (i % j == 0)  
       {  
         counter++;  
         break;  
       }  
     }  
     if (counter == 0 && i != 1)  
     {  
       Console.WriteLine(i);  
     }  
   }  
   Console.ReadKey();  
 }  

Prime Number

Output
 Enter the start number: 1  
 Enter the end number: 50  
 The prime number/s of given range  
 2  
 3  
 5  
 7  
 11  
 13  
 17  
 19  
 23  
 29  
 31  
 37  
 41  
 43  
 47  

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال