Prime number program in c using while loop

Prime number program in c using while loop:

 Prime number program in c using while loop:
#include<stdio.h>
int main()
{
    int i,j,k;
    printf("Enter a number: ");
    scanf("%d",&i);
    k=0;
    j=2;
    while(j <= i/2)
    {
        if(i%j == 0)
        {
            k=1;
            break;
        }
        j++;
    }
    if(k==0)
        printf("The entered number is Prime Number.");
    else
        printf("Entered number is Not Prime Number.");
    return 0;

}
Output:
Enter a number: 7

The entered number is Prime Number.

Explanation of the above program: 

  1. If the while loop condition (j <= i/2) is true then loop enters into while loop and check the if condition (i%j == 0). 
  2. If the condition is true the Entered number is not the prime number. 
  3. If the condition is False, then the number entered is the Prime number.
Read also: 

Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...

Blog Archive