#include<stdio.h>
#include<stdlib.h>
int main()
{
int choice,num,i,fact;
while(1)
{
printf("\n 1-factorial\n");
printf("\n2-prime\n");
printf("\n3.odd/even\n");
printf("\n4-exit\n");
printf("your choice?\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter a number");
scanf("%d",&num);
fact=1;
for(i=1;i<=num;i++)
{
fact=fact*i;//factorial of a number//
}
printf("factorial value is %d\n",fact);
break;//ends the loop here//
case 2:
printf("enter a number");
scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i==0)
{
printf("the number is not a prime number\n");
break;
}
if(i==num)
{
printf("the number is prime\n");
break;
}
}
case 3:
printf("enter a number");
scanf("%d",&num);
if(num%2==0)
{
printf("the number is even\n");
}
else
{
printf("the number is odd\n");
break;
}
case 4:
exit(0);//terminates program execution included in the <stdlib.h>//
default:
printf("the choice is invalid\a\n");//\a means alarm//
}
}
return 0;
}
i m not sure about the while loop like i have tried for(;;) too but i dont think thats the issue when i input 1 the factorial part works fine but when i press 2 or 3 it says - enter a number and when i enter one it again prompts me to enter a number and then breaks what maybe the issue here
i tried changing some logics but that didn't help i,googled it and it said something about buffer of \n but i couldn't understand it