C program to check given number is prime or not

 

 C program to check given number is prime or not


/*************************************************************

 C program to check given number is prime or not

 ***********************************************************/

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

     int i,n,count=0;

     printf("Enter number to check it is prime or not\n");

     scanf("%d",&n);

     for(i=1;i<=n;i++)

     {

         if(n%i==0)

         

         count++;

         

     }

     if(count==2)

     printf("Given number %d is prime number\n",n);

     else

     printf("Given number %d is not prime number\n",n);

     printf("End of program");

 }


Enter number to check it is prime or not

2

Given number 2 is prime number

End of program

Enter number to check it is prime or not

7

Given number 7 is prime number

End of program


To check given number is prime or not the mathematical logic is

if number is divisible by 1 and itself then the number is prime number.

for example number 7

then 7/1=0

and 7/7=0

so 7 is prime number. The given number should be divisible by only 1 and itself.


टिप्पणी पोस्ट करा

0 टिप्पण्या