Welcome...

Monday, 16 September 2013

C program to find whether a given number is prime or not

Following program reads a number from the user and prints whether it is prime or not along with the factors of the given number:

/*Program to find the given number is prime number or not*/
main()
{
    int number,i,count=0;
    clrscr();
    printf("Enter the number:");
    scanf("%d",&number);
    printf("\nFactors of %d are:",number);
    for(i=1;i<=number;i++)
    {
        if(number%i==0)
        {
            printf("\n%d",i);
            count++;
        }
    }
    if(count>2)
    {
        printf("\n\n%d is not a prime number",number);
    }
    else if(number<=1)
    {
        printf("\n\n%d is invalid!",number);
    }
    else
    {
        printf("\n\n%d is a prime number",number);
    }
    getch();
}
Sample input and output:
Enter the number:13

Factors of 13 are:
1
13

13 is a prime number

No comments:

Post a Comment