C program to Prime Number

Below is C program to Prime Number.

Program: Prime Number using while loop

/* Prime Number using while loop */

#include<stdio.h>
int main()
{
    int a,i,f;
    printf("Enter a number: ");
    scanf("%d",&a);
    f=0;
    i=2;
    while(i <= a/2)
    {
        if(a%i == 0)
        {
            f=1;
            break;
        }
        i++;
    }
    if(f==0)
        printf("Prime Number")
    else
        printf("Not Prime Number");
 return 0;
}

Program: Prime Number using for loop

/* Prime number using for loop */

#include<stdio.h>
int main()
{
    int a,i,f;
    printf("Enter a number: ");
    scanf("%d",&a);
    f=0;
    for(i=2;i <= a/2;i++)
    {
        if(a%i == 0)
        {
            f=1;
            break;
         }
    }
    if(f==0)
        printf("Prime Number")
    else
        printf("Not Prime Number");
 return 0;
}

No comments:

Post a Comment