C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Decision Making Programs in C

Menu-Driven Number Check of Positive, Negative, or Zero and Even or Odd

Introduction

Menu-driven programs are very useful in real-world applications where the user chooses what task to perform.
Here, we’ll use a switch-case to provide options for number checking.

Menu-driven program in C where the user can choose what they want to check:

  1. Check Positive/Negative/Zero
  2. Check Even/Odd
  3. Check Both
  4. Exit

 

C Program: Menu-Driven Number Check of Positive, Negative, or Zero and Even or Odd

C

#include <stdio.h>

 

int main() {

    int num, choice;

 

    do {

        // Display menu

        printf("\n===== Number Checking Menu =====\n");

        printf("1. Check Positive/Negative/Zero\n");

        printf("2. Check Even/Odd\n");

        printf("3. Check Both (Sign and Even/Odd)\n");

        printf("4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        if (choice == 4) {

            printf("Exiting program. Goodbye!\n");

            break;

        }

 

        // Input number only if choice is valid

        printf("Enter a number: ");

        scanf("%d", &num);

 

        switch (choice) {

            case 1:

                if (num > 0)

                    printf("%d is Positive.\n", num);

                else if (num < 0)

                    printf("%d is Negative.\n", num);

                else

                    printf("The number is Zero.\n");

                break;

 

            case 2:

                if (num == 0) {

                    printf("The number is Zero (neither Even nor Odd).\n");

                } else if (num % 2 == 0)

                    printf("%d is Even.\n", num);

                else

                    printf("%d is Odd.\n", num);

                break;

 

            case 3:

                if (num == 0) {

                    printf("The number is Zero (neither Positive/Negative nor Even/Odd).\n");

                } else {

                    if (num > 0)

                        printf("%d is Positive and ", num);

                    else

                        printf("%d is Negative and ", num);

 

                    if (num % 2 == 0)

                        printf("Even.\n");

                    else

                        printf("Odd.\n");

                }

                break;

 

            default:

                printf("Invalid choice! Please enter 1–4.\n");

        }

 

    } while (choice != 4);

 

    return 0;

}

Output

 
OUTPUT :
===== Number Checking Menu =====
1. Check Positive/Negative/Zero
2. Check Even/Odd
3. Check Both (Sign and Even/Odd)
4. Exit
Enter your choice: 3
Enter a number: -8
-8 is Negative and Even.

===== Number Checking Menu =====
1. Check Positive/Negative/Zero
2. Check Even/Odd
3. Check Both (Sign and Even/Odd)
4. Exit
Enter your choice: 2
Enter a number: 0
The number is Zero (neither Even nor Odd).

===== Number Checking Menu =====
1. Check Positive/Negative/Zero
2. Check Even/Odd
3. Check Both (Sign and Even/Odd)
4. Exit
Enter your choice: 4
Exiting program. Goodbye!
 

Explanation

  1. Program shows a menu repeatedly using a ..while loop.
  2. switch-case handles user’s choice.
  3. User enters a number → program checks conditions.
  4. Option 4 exits the loop and program ends.