Menu-Driven Calculator
5/5 - (1 vote)

In the world of programming, creating a calculator or menu-driven program is a rite of passage for many beginners. It’s a simple yet essential project that helps you grasp the fundamentals of coding. In this blog, we’ll explore a basic calculator program written in the C programming language. This program allows users to perform four fundamental arithmetic operations: addition, subtraction, multiplication, and division.

The Code:

Let’s start by breaking down the provided C code step by step.

#include <stdio.h>
int main() {
    int x, a, b, c;
    char ch;

    printf("Enter according to your choice\n");
    printf("1 for addition\n");
    printf("2 for subtraction\n");
    printf("3 for multiplication\n");
    printf("4 for division\n");

    do {
        printf("Enter here : ");
        scanf("%d", &x);
        printf("Enter  a and b : ");
        scanf("%d%d", &a, &b);

        switch (x) {
            case 1: c = a + b;
                printf("Result is %d\n", c);
                break;
            case 2: c = a - b;
                printf("Result is %d\n", c);
                break;
            case 3: c = a * b;
                printf("Result is %d\n", c);
                break;
            case 4: c = a / b;
                printf("Result is %d\n", c);
                break;
            default: printf("Wrong input\n");
        }

        printf("Do you want to continue?\n");
        printf("Enter y for yes and n for no : ");
        scanf(" %c", &ch); //white space character is necessary
    } while (ch == 'y' || ch == 'Y');

    return 0;
}

How the Code Works:

  1. stdio.h is defined as a standard input/output library which is included at the start of the program and defines the main function.
  2. It declares several variables: x to store the operation choice, a and b to store operands, c to store the result, and ch to store the user’s decision to continue or exit.
  3. The program presents a menu of options to the user, allowing them to choose from four basic operations: addition, subtraction, multiplication, and division.
  4. Inside a do-while loop, the user is prompted to enter their choice (1-4) and the operands (a and b).
  5. A switch-case statement is used to perform the selected operation and display the result. If the user enters any number greater than 4 it displays “Wrong Input”.
  6. After performing the operation, the user is asked if they want to continue. If they input ‘y’ or ‘Y’, the loop continues; otherwise, the program exits.

Output :

The output of the provided C code will depend on the user’s input during program execution. Here is a step-by-step breakdown of how the program works and the expected output for different inputs:

  1. The program starts by displaying the following options:
Enter according to your choice
1 for addition
2 for subtraction
3 for multiplication
4 for division

2. The program then enters a do-while loop, where it prompts the user to enter their choice, a value for ‘a’, and a value for ‘b’. For example, if the user chooses addition (1) and enters ‘5’ for ‘a’ and ‘3’ for ‘b’, the program will proceed as follows:

Enter here : 1
Enter  a and b : 5 3
Result is 8
Do you want to continue?
Enter y for yes and n for no : 
  1. The program displays the result of the chosen operation, which is the addition of 5 and 3, yielding 8. It then displays “Do you want to continue?” If the user enters ‘y’ (for yes) or ‘Y’, the program continues to the menu. If they enter ‘n’ (for no), the program exits.
  2. If the user enters ‘2’ for subtraction, and ‘7’ for ‘a’ and ‘4’ for ‘b’, the program will look like this:
Enter here : 2
Enter  a and b : 7 4
Result is 3
Do you want to continue?
Enter y for yes and n for no : 

The program performs subtraction, and the result is 3 (7 – 4).

  1. If the user enters ‘3’ for multiplication, and ‘6’ for ‘a’ and ‘9’ for ‘b’, the program will produce:
Enter here : 3
Enter  a and b : 6 9
Result is 54
Do you want to continue?
Enter y for yes and n for no : 

The program performs multiplication, and the result is 54 (6 * 9).

  1. If the user enters ‘4’ for division, ’10’ for ‘a’ and ‘2’ for ‘b’, the program will display:
Enter here : 4
Enter  a and b : 10 2
Result is 5
Do you want to continue?
Enter y for yes and n for no : 

The program performs division, and the result is 5 (10 / 2).

  1. If the user enters any other value apart from 1, 2, 3, or 4, the program will print “Wrong input” and prompt the user to continue.
  2. To exit the program, the user can enter ‘n’ (for no) when asked if they want to continue. The program will terminate.
  3. The program will continue to loop as long as the user enters ‘y’ or ‘Y’ when asked if they want to continue, allowing them to perform additional calculations.

Why White Space Character is Necessary Before %c for Menu-Driven Programs in C Programming?

A white space character is necessary before %c for menu-driven programs in C programming because it allows the program to ignore any white space characters (such as spaces, tabs, or newlines) that may be present in the input buffer before reading the actual character for the menu option. This ensures that the program can correctly identify the user’s choice and execute the corresponding action.

For example, suppose you have a menu-driven program that asks the user to enter one of these options: A, B, C, or Q (to quit). If you use scanf (“%c”, &ch) to read the user’s input, then the program may read a newline character (\n) that was left in the input buffer from the previous input. This will cause the program to skip the menu option and execute an invalid action. To avoid this problem, you can use scanf (” %c”, &ch) to skip any white space characters before reading the menu option.

Why It is important to use a do-while loop while making Menu-driven Programs?

It is important to use a do-while loop while making menu-driven programs in C programming because it ensures that the program will execute the menu at least once before checking the user’s choice. This way, the user can see the available options and select one of them. If the user enters an invalid choice, the program can display an error message and repeat the menu until the user enters a valid choice or exits the program.

Read more from us: Swapping Variables in C Using 3 Methods: An Amazing Guide (codershot.com)

See this YouTube video for more convenience:

5 1 vote
Course Rating

Share:

More Posts

Send Us A Message

0
Would love your thoughts, please comment.x
()
x