CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine all parts into a working calculator.

Putting it together

In the final step we'll add multiply and divide options to the menu. The calculator will then be a complete app supporting all four operations:

  • case 1: addition
  • case 2: subtraction
  • case 3: multiplication
  • case 4: division

We'll keep the existing case 1 and case 2 branches and add case 3 and case 4.

Practice Add case 3 and case 4

Add case 3: printf("%d\n", multiply(5, 3)); break; and case 4: printf("%d\n", divide(6, 3)); break; branches to the menu below. The calculator will then support all four operations.

Starter code

main.c
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

int divide(int a, int b) {
    if (b == 0) return 0;
    return a / b;
}

int main(void) {
    printf("Calculator\n");
    int choice = 1;
    while (choice != 0) {
        switch (choice) {
            case 1: printf("%d\n", add(5, 3)); break;
            case 2: printf("%d\n", subtract(5, 3)); break;
            default: break;
        }
        choice = 0;
    }
    return 0;
}

Sample solution

main.c
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

int divide(int a, int b) {
    if (b == 0) return 0;
    return a / b;
}

int main(void) {
    printf("Calculator\n");
    int choice = 1;
    while (choice != 0) {
        switch (choice) {
            case 1: printf("%d\n", add(5, 3)); break;
            case 2: printf("%d\n", subtract(5, 3)); break;
            case 3: printf("%d\n", multiply(5, 3)); break;
            case 4: printf("%d\n", divide(6, 3)); break;
            default: break;
        }
        choice = 0;
    }
    return 0;
}

Expected output

Calculator
8

Project complete

Congratulations! You've reached the end of the five-lesson project. You wrote a complete calculator app:

  • Four functions: add, subtract, multiply, divide
  • A divide-by-zero check
  • A menu with a while loop and switch

Next you can add real user input with scanf, error handling and more operations. Everything you build in C from now on will rest on these foundations.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 case label

Question
switch (choice) {
    ____ printf("%d", add(5, 3)); break;
}
Answer
switch (choice) {
    case 1: printf("%d", add(5, 3)); break;
}

Explanation: The case 1: label runs when choice is 1.

2 Why use switch in a menu?

  • For multi-branch selection Correct answer
  • For loops
  • For computation
  • For output

Explanation: switch picks a branch based on a value; ideal for a menu.

3 Match all concepts

Term Meaning
add Addition
subtract Subtraction
multiply Multiplication
divide Division

All lessons in Beginner