Q:

Write a C program to check whether a number is Prime number or not using while & for loop

0

Write a C program to check whether a number is Prime number or not using while & for loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Check whether a number is Prime number or not using while loop

#include<stdio.h>
int main()
{
    int num, i, f;

    //Readind a number from user
    printf("Enter any number: ");
    scanf("%d", &num);

    f = 0;
    i = 2;
    while(i <= num/2)
    {
        if(num%i == 0)
        {
            f=1;
            break;
        }
        i++;
    }
    if(f == 0)
        printf("%d is a Prime Number", num);
    else
        printf("%d is Not a Prime Number", num);
 return 0;
}

Check whether a number is Prime number or not using for loop

#include<stdio.h>
int main()
{
    int num, i, f;

    //Reading a number from user
    printf("Enter a number: ");
    scanf("%d",&num);
    f=0;
    for(i=2; i <= num/2; i++)
    {
        if(num%i == 0)
        {
            f=1;
            break;
        }
    }
    if(f==0)
        printf("%d is a Prime Number", num);
    else
        printf("%d is a Not Prime Number", num);
    return 0;
}

Result:

Enter a number: 19

19 is a Prime Number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C program to check whether a number is pal... >>
<< Write a C program to calculate compound Interest...