Subscribe

RSS Feed (xml)

Friday, February 13, 2009

Write a C program to check whether the given integer Prime number or not.

Method: 1

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, flag=0, x=2;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no/2) {
if(no%x==0)
{
flag=1;
break;
}
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}

Output:-
Enter the required number: 23
Prime number.

Method: 2 // Prime number or not

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, ct=0 , x=1;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no)
{

if(no%x==0)
{ ct++; }
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}

Output:-
Enter the required number: 21
Not a Prime number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

1 comment: