Write a program to create a basic calculator with menus in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (1)Space Complexity: O (1)Open Source: MIT
1
2
    #include <stdio.h>
3
    #include <conio.h>
4
    #include <stdlib.h>
5
6
    void main()
7
    {
8
        int num1, num2, result, choice;
9
        clrscr();
10
        printf("\nEnter two numbers\n");
11
        scanf("%d%d", &num1, &num2);
12
    
13
        printf("\n*******************\n");
14
        printf("* 1. Swapping       *\n");
15
        printf("* 2. Addition       *\n");
16
        printf("* 3. Subtraction    *\n");
17
        printf("* 4. Multiplication *\n");
18
        printf("* 5. Division       *\n");
19
        printf("* 6. Exit           *\n");
20
        printf("*******************\n");
21
        do
22
        {
23
            printf("\n\nMake Your Choice:");
24
            scanf("%d", &choice);
25
        
26
            switch (choice)
27
            {
28
            case 1:
29
            
30
                result = num1;
31
                num1 = num2;
32
                num2 = result;
33
                printf("\nThe Swipping is %d %d", num1, num2);
34
                break;
35
            case 2:
36
                result = num1 + num2;
37
                printf("\n The Addition of %d and %d is %d", num1, num2, result);
38
                break;
39
            case 3:
40
                result = num1 - num2;
41
                printf("\n The Subtraction of %d and %d is %d", num1, num2, result);
42
                break;
43
            case 4:
44
                result = num1 * num2;
45
                printf("\n The Multiplication of %d and %d is %d", num1, num2, result);
46
                break;
47
            case 5:
48
                result = num1 / num2;
49
                printf("\n The Division of %d and %d is %d", num1, num2, result);
50
                break;
51
            case 6:
52
                exit(0);
53
                break;
54
            default:
55
                printf("\nWrong Entry\n ");
56
                break;
57
            }
58
          
59
        } while (1);
60
    }
61
        

Output

1
2
    Enter two numbers
3
    5
4
    7
5
6
    *******************
7
    * 1. Swapping       *
8
    * 2. Addition       *
9
    * 3. Subtraction    *
10
    * 4. Multiplication *
11
    * 5. Division       *
12
    * 6. Exit           *
13
    *******************
14
15
16
    Make Your Choice:1
17
18
    The Swipping is 7 5
19
20
    Make Your Choice:2
21
22
    The Addition of 7 and 5 is 12
23
24
    Make Your Choice:6
25
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run basic calculator with menus program in C-Language?

To run basic calculator with menus program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about a Menu-Driven Calculator using the C Language

Aim: To create a menu-driven calculator that accepts user input to perform basic arithmetic operations, such as Addition, Subtraction, multiplication, and division, on two variables.

Concepts used in this program

  • Switch case: To execute the different operations at the basis of user choice.
  • Do-While loop: To run the calculator again & again so that the user can perform multiple operations without re-executing the program.
  • Arithmetic Operators: To perform the Addition (+), Subtraction (-), Multiplication (*) and Division (/).
  • Exit Function: To terminate the program, the exit (0) method is used.
  • Default case: To show the error message when choosing the wrong menu option.

Real-Life Applications

  • In Basic Calculators: Basic calculators have an extended version of this calculator program. This is used by millions of users on a daily basis.
  • Operating System Default Calculators: Almost every OS has its own default calculator with at least these 4 basic operations.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.