Sunday 17 July 2011

C programme for prime or not

USING FOR LOOP
#include <stdio.h>
#include <conio.h>
 main()
{
int n,i,s;
printf("enter n");
scanf("%d",&n);
for(i=1,s=0;i<=n;i++)
{
if(n%i=0)
s++;
}
if(s==2)
printf(" n is prime");
else
printf(" entered numb is not prime");
getch();
}

USING WHILE LOOP



#include <stdio.h>
#include <conio.h>
void main()
{
int n,i=2;
printf("enter numb");
scanf("%d",&n);
while(n%i!=0)
{
i=i+1;
}
if(n==i)
printf("prime numb");l
else
printf("not prime");
}

No comments:

Post a Comment