write an interacting c program that will accept a positive integer quantity to perform any one operation at a time , list of operations: "odd or even" " factorial" " investigating the digits " based on the user choice . the program will execute repeatedly , until a value ' n/N ' read in from the keyboard.
Answers
Explanation:
I need answer for this question
Answer:
Here's an example of an interactive C program that accepts a positive integer and performs an operation based on user choice:
#include <stdio.h>
int main() {
int num, choice, i, fact = 1, digit, sum = 0;
char repeat;
do {
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Enter choice of operation:\n");
printf("1. Odd or Even\n");
printf("2. Factorial\n");
printf("3. Investigating the digits\n");
scanf("%d", &choice);
switch (choice) {
case 1:
if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}
break;
case 2:
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (i = 1; i <= num; i++) {
fact = fact * i;
}
printf("Factorial of %d is %d.\n", num, fact);
}
break;
case 3:
while (num > 0) {
digit = num % 10;
sum = sum + digit;
num = num / 10;
}
printf("The sum of digits of %d is %d.\n", num, sum);
break;
default:
printf("Invalid choice. Please enter a valid choice.\n");
break;
}
printf("Do you want to perform another operation? (y/n) ");
scanf(" %c", &repeat);
} while (repeat != 'n' && repeat != 'N');
return 0;
}
Explanation:
This program uses a do-while loop to repeatedly prompt the user for a positive integer and choice of operation. It then performs the selected operation and asks the user if they want to perform another operation. If the user enters 'n' or 'N', the loop ends and the program exits.
To learn more about do-while loop from the link below
https://brainly.in/question/9705104
To learn more about positive integer from the link below
https://brainly.in/question/13402769
#SPJ3