Search C Program

Prime Number

/* 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;
}

-------------------------------------------------------

/* 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;
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. #include
    #include
    int isPrime(long num)
    {
    int i,num_sqrt=sqrt(num)+1;
    if(num<2) return 0;
    if(num==2 || num==3) return 1;
    if(num%2==0 || num%3==0) return 0;
    for(i=6;i<=num_sqrt;i+=6)
    if(num%(i-1)==0 || num%(i+1)==0) return 0;
    return 1;
    }

    int main()
    {
    int num;
    printf("Enter a number :");
    scanf("%d",&num);
    if(isPrime(num))
    printf("Prime number");
    else
    printf("Not prime number");
    return 0;
    }

    ReplyDelete