top of page

C Program to check Prime Number

Writer's picture: Prateek ChauhanPrateek Chauhan

First of all a prime number is a positive integer that is divisible only by 1 and itself.

For example: 2. 3, 5, 7, 11, 13, 17

 

Implementation Using C:

#include<stdio.h>
int main()
{
    int i, n, flag = 1;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    
    for(i = 2; i <= n/2; i++)
    {
        if(n % i == 0)
        {
            flag = 0;
            break;
        }
    }
    if(n == 1)
        printf("1 is neither prime nor composite");     
    else{         
        if(flag == 1)          }
            printf("%d is a prime number", n);
        else
            printf("%d is not a prime number", n);
    }
    return 0;
}
Output:
Enter a positive integer: 19
19 is a prime number
 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Follow Programmers Door for more.

5 views0 comments

Recent Posts

See All

Comments


bottom of page