Code Box
----------------------------------------------------------
/*Program to find input number is prime or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,c;
c=0; //counter
clrscr();
printf("\nEnter any number:");
scanf("%d",&num);
clrscr(); //for clear screen
printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
if(num==0)
{
printf("\nZero entered!!"); //Zero case
}
else
{
/*Prime finder */
for(i=2;i<=(num/2);i++)
{ /*start from 2 because every number is divisible by 1 */
/* i<=(num/2) because any number can be completely divisible by only half or less than itself.*/
if(num%i==0)
{
c=1;
}
}
/*Prime checker */
if(c==0)
{
printf("\n%d is prime",num);
}
else{
printf("\n%d is not a prime",num);
}
}
printf("\n------------------------------");
getch();
}
No comments:
Post a Comment