C program to check entered number is prime or not

Number prime

USing For loop
#include<stdio.h>
#include<conio.h>
void main()
{
 int x,y=1,i;
 clrscr();
 printf("Enter Number:- ");
 scanf("%d",&x);
 for(i=1;i<=x/2;i++)
  {
   if(x%i==0)
    {
     y++;
    }
  }
 if(y==2)
 {
  printf("Number is prime");
 }
 else
 {
   printf("Number is not prime");
 }
getch();
}
Using While Loop
#include<stdio.h>
#include<conio.h>
void main()
{
 int x,y=1,i=1;
 clrscr();
 printf("Enter Number:- ");
 scanf("%d",&x);
 while(i<=x/2)
  {
   if(x%i==0)
    {
     y++;

    }
    i++;
  }
 if(y==2)
 {
  printf("Number is prime");
 }
 else
 {
   printf("Number is not prime");
 }
getch();
}

OUTPUT:-

When Number is Prime

number prime

When Number is Not Prime

number prime

Leave a Reply

Your email address will not be published. Required fields are marked *