C Program to check prime number

C Program to  check prime number


 #include<conio.h>
#include<stdio.h>
void main()
{
    int p,i;
    clrscr();
    printf("enter no. to check prime: ");
    scanf("%d",&p);
    for(i=2;i<p;i++)
    if(p%i==0)
    {
        printf("%d is not a prime number ",p);
        break;
    }
    if(i==p)
    printf("%d is prime number",p);
    getch();
}



Explanation

  • int p,i;
      p and i are two variables of integer type ;
  • printf() and scanf() are predefine functions use for read and write respectively.
  • for(i=2;i<p;i++)
    • for loop use to check prime number  count
    •  if(p%i== 0)
      • if p % i = 0 then it means 'p' has any common factor between 1 to p which means it is not a prime number 
    • once we get not a prime number condition then we can not we will stop the loop by using 'break' statement 
    • End the execution. 
   



Output:

Comments

Popular posts from this blog

SKILL ROCKS TECH QUESTIONS

C Program for Analog Clock Design

Php Check is date or not