Check if a Number is Prime
In this step, we will check if a number is prime or composite. The program will output a message indicating whether the number entered by the user is prime or not.
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Factorial \n");
printf("2. Prime\n");
printf("3. Odd\\Even\n");
printf("4. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
/*
* Code to calculate factorial of a number
*/
break;
case 2:
printf("Enter number:\n");
scanf("%d", &num);
if(num == 1)
printf("\n1 is neither prime nor composite\n\n");
for(i = 2; i < num; i++)
{
if(num%i == 0)
{
printf("\n%d is not a prime number\n\n", num);
break;
}
}
/*
Not divisible by any number other
than 1 and itself
*/
if(i == num)
{
printf("\n\n%d is a Prime number\n\n", num);
break;
}
break;
case 3:
/*
* Code to check if a number is even or odd
*/
break;
case 4:
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
exit(0);
}
}
return 0;
}