Check Prime or Not - C Program

Program:

#include<stdio.h>
//#include<conio.h>
#include<math.h>
main()
{
    int n,i,flag=0;//clrscr();
    printf("Enter the number:");
    scanf("%d",&n);
    if(n<=1)
        printf("\nThe no is not prime");
    else
    {
        for(i=2;i<=(n/2);i++)
            {
             if(n%i==0)
             flag=1;
            }

        if (flag==0)
            printf("%d is prime.\n",n);
        else
            printf("%d is not prime.\n",n);
    }
//getch();
}
Output:
nn@linuxmint ~ $ gcc c2.c
nn@linuxmint ~ $ ./a.out
Enter the number:2
2 is prime.
nn@linuxmint ~ $ ./a.out
Enter the number:5   
5 is prime.
nn@linuxmint ~ $ ./a.out
Enter the number:9
9 is not prime.
nn@linuxmint ~ $

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...