Answer to Question #309210 in C for Nobody

Question #309210

Write a menu driven program which has the following options:

1. Factorial of a number

2. Prime or not

3. Odd or Even

4. Exit


Once a menu item is selected the appropriate action should be taken and once this action is finished, the menu should reappear. Unless the user selects the “Exit” option the program should continue to work. You may use of an infinite while and switch statement.


1
Expert's answer
2022-03-10T18:07:34-0500




#include<stdio.h>
#include<math.h>




int isPrime(int number){
	int i;
	if (number < 2){
		return 0;
	}
	if (number == 2 || number == 3){
		return 1;
	}
	for (i = 2; i <= sqrt(number * 1.0); i++){
		if (number % i == 0){
			return 0;
		}
	}
	return 1;
}




int main(){
	int choice=-1;
	int n,i=1;
	int factorial=1;


	while(choice!=4){
		printf("1. Factorial of a number\n");
		printf("2. Prime or not\n");
		printf("3. Odd or Even\n");
		printf("4. Exit\n");
		printf("Your choice: ");
		scanf("%d",&choice);


		switch(choice){
		case 1:
			{
				printf("Enter number: ");
				scanf("%d",&n);
				while(i<=n){
					factorial*=i;
					i++;
				};
				printf("The factorial of is %d.\n\n",factorial);
			}
			break;
		case 2:
			{
				printf("Enter number: ");
				scanf("%d",&n);
				if(isPrime(n)){
					printf("The number is prime\n\n");
				}else{
					printf("The number is NOT prime\n\n");
				}
			}
			break;
		case 3:
			{
				printf("Enter number: ");
				scanf("%d",&n);
				if(n%2==0){
					printf("The number is even.\n\n");
				}else{
					printf("The number is odd.\n\n");
				}
			}
			break;
		case 4:
			break;
		default:
			printf("Wrong menu item\n\n");
			break;
		}
	}


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS