I wrote a program as follows:
#include <stdio.h>
int main() {
int i, choice, a, mul=1, j;
printf("Enter a number: ");
scanf("%d ", &i);
printf("\nEnter corresponding number to perform given operation:");
printf("1) Factorial of the number");
printf("2) Check if prime or not");
printf("3) Check whether Odd or Even");
printf("4) Exit");
scanf("%d ", &choice);
switch (choice) {
case 1:
for( a=i; a>0; a--) {
mul*=a;
}
printf("\n%d! = %d", i, mul);
break;
case 2:
for(a=1; a<=((i+1)/2); a++) {
if((i%a)==0) {
j++;
}
}
if(j==1) {
printf("%d is a prime number",i);
} else {
printf("%d is NOT a prime number",i);
}
break;
case 3:
if((i%2)==0) {
printf("%d is EVEN", i);
} else {
printf("%d is ODD", i);
}
break;
case 4:
;
}
return 0;
}
When I ran the program it ran the first few lines correctly, that is:
printf("Enter a number: ");
scanf("%d ", &i);
But then it stopped, then and there. I pressed enter a few times but it didn't seem to work. Then I just entered '1' and then it started executing the next statements. Then it asked for choice':
scanf("%d ", &choice);
I entered '1' again and then it executed that case. I don't know why the program stopped after the first scanf statement. And resumed after entering a value.
The output screen was as follows:
Enter a number: 12
1
Enter the corresponding number to perform given operation:
1)Factorial of the number
2)Check if prime or not
3)Check whether Odd or Even
4)Exit1
12! = 479001600