Check whether a number is a Prime Number

 Prime Number


C Program
1.For Loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,count;
    printf("Enter any Number:");
    scanf("%d",&n);
    int i=1;
    count=0;
    for(i=1;i<=n;i++)
    {
        if(n%i==0)
            count++;
    }
    if(count==2)
        printf("Prime");
    else
        printf("Not Prime");
    
}
2.While Loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,count;
    printf("Enter any Number:");
    scanf("%d",&n);
    int i=1;
    count=0;
    while(i<=n)
    {
        if(n%i==0)
            count++;
        i++;
    }
    if(count==2)
        printf("Prime");
    else
        printf("Not Prime");
 }
3.Do While Loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,count;
    printf("Enter any Number:");
    scanf("%d",&n);
    int i=1;
    count=0;
    do
     {
      if(n%i==0)
            count++;
        i++;
     }while(i<=n);

    if(count==2)
        printf("Prime");
    else
        printf("Not Prime");
}


Java Program

0 Comments:

Post a Comment