Search This Blog

Check a number is prime or not | c programing

Check a number is prime  or not

 

Problem

Read a integer value, which a natural number. Find the the number is a prime number or not.


Input

The input file contains a integer value.

Output

Print the number is a prime number or not.





Solution 

 A prime number is a positive integer which is evenly divisible only by 1 and itself. Remember, 0 &1 are not considered as prime number.

 To check the number is prime or not, we will divide the number from 2 to its half and check if it is evenly divided or not.

#include<stdio.h>

int main()
{
    int i,N,flag = 0;
    scanf("%d",&N);

    if(N==0 || N==1){
        printf("Not prime\n");
    }
    else{
            for(i=2;i<=N/2;i++){
                    if(N%i == 0){
                        flag = 1; ///not prime
                        break;
                    }
            }
        if(flag == 0){
            printf("prime\n");
        }
        else{
            printf("not prime\n");
        }
    }


    return 0;
}
 

No comments

Powered by Blogger.