Write a program to demonstrate the following menu-driven program. The user will provide an integer and alphabet for making choice and the corresponding task has to be performed according as follow:

  1. Find Odd or Even
  2. Find Positive or Negative
  3. Find the Factorial value
  4. Exit

The choice will be displayed until the user will give “D” as a choice.

This answer is restricted. Please login to view the answer of this question.

Login Now
#include<stdio.h>

void oddeven(int num);
void posneg(int num);
void fact(int num);

int main(){

    int number;
    char choice;

    do{
        printf("A. Find Odd or Even\nB. Find Positive or Negative\nC. Find the Factorial value\nD.Exit");
        printf("\n\nEnter your choice: ");
        scanf(" %c", &choice);

        if( choice != 'D' ){
            printf("Enter a number: ");
            scanf(" %d", &number);
        }

        switch(choice){
            case 'A':
                oddeven(number);
                break;

            case 'B':
                posneg(number);
                break;

            case 'C':
                fact(number);
                break;

            case 'D':
                printf("\nExiting program\n");
                break;
        }


    }while( choice != 'D' );

    return 0;
}

void oddeven(int num){
    if( num % 2 == 0 ){
        printf("\n\n================\n%d is even number\n================\n\n", num);
    }else{
        printf("\n\n================\n%d is odd number\n================\n\n", num);
    }
}

void posneg(int num){
    if( num >= 0 ){
        printf("\n\n================\n%d is positive number\n================\n\n", num);
    }else{
        printf("\n\n================\n%d is negative number\n================\n\n", num);
    }
}

void fact(int num){
    int i = 0, factorial = 1;
    for( i = 1; i <= num; i++){
        factorial *= i;
    }
    printf("\n\n================\nFactorial of %d = %d\n================\n\n", num, factorial);
}

The output of above program is

A. Find Odd or Even
B. Find Positive or Negative
C. Find the Factorial value
D.Exit

Enter your choice: A
Enter a number: 5


================
5 is odd number
================

A. Find Odd or Even
B. Find Positive or Negative
C. Find the Factorial value
D.Exit

Enter your choice: B
Enter a number: 5


================
5 is positive number
================

A. Find Odd or Even
B. Find Positive or Negative
C. Find the Factorial value
D.Exit

Enter your choice: C
Enter a number: 5


================
Factorial of 5 = 120
================

A. Find Odd or Even
B. Find Positive or Negative
C. Find the Factorial value
D.Exit

Enter your choice: D

Exiting program
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .