Answer to Question #311474 in C for Daniel

Question #311474

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



1
Expert's answer
2022-03-14T13:04:41-0400


#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