Menu Driven Calculator Program in C Using Functions

In the control flow, we created a simple calculator program using a switch case statement. Now, we will create a calculator program in C using functions.

Functions are used to divide a big problem into small subroutines. In this program, the user has the choice for operation, and it will continue until the user doesn’t want to exit from the program.

Prerequisites for this program:- Introduction to Function in C, User-defined Functions in C, C Program Using Functions Example

In this program, the calculator will perform add, subtract, multiply, divide, remainder, and power. To perform every operation one function will be created. Another function display() is used to print the result.

calculator program in c using functions
calculator program in c using functions

Calculator program in C using functions

#include<stdio.h>
#include<stdlib.h>

// function declarations
void display(float n1, float n2, char ch, float result);
void add(float n1, float n2);
void subtract(float n1, float n2);
void multiply(float n1, float n2);
void divide(float n1, float n2);
void rem(float n1, float n2);
void power(float n1, float n2);

// main function
int main()
{
  float n1, n2;
  int ch;

  do{
    printf("Enter two numbers: ");
    scanf("%f %f", &n1, &n2);

    printf("\n*****************");
    printf("\n1.Addition");
    printf("\n2.Subtraction");
    printf("\n3.Multiplication");
    printf("\n4.Division");
    printf("\n5.Remainder");
    printf("\n6.Power (x^y)");
    printf("\n7.Exit");
    printf("\nEnter your choice: ");
    scanf("%d", &ch);

    switch (ch) {
      case 1:
        add(n1,n2);
        break;
      case 2:
        subtract(n1,n2);
        break;
      case 3:
        multiply(n1,n2);
        break;
      case 4:
        divide(n1,n2);
        break;
      case 5:
        rem(n1,n2);
        break;
      case 6:
        power(n1,n2);
        break;
      case 7:
        printf("Thank You.");
        exit(0);
      default:
        printf("Invalid input.");
        printf("Please enter correct input.");
    }

    printf("\n**********************************\n");
  }while(1);

  return 0;
}

// function for displaying the result
void display(float n1, float n2, char ch, float result)
{
  printf("%.2f %c %.2f = %.2f\n", n1, ch, n2, result);
}

// function for addition of two numbers
void add(float n1, float n2)
{
  float result = n1 + n2;
  display(n1, n2, '+', result);
}

// function for subtraction of two numbers
void subtract(float n1, float n2)
{
  float result = n1 - n2;
  display(n1, n2, '-', result);
}

// function for multiplication of two numbers
void multiply(float n1, float n2)
{
  float result = n1 * n2;
  display(n1, n2, '*', result);
}

// function for division of two numbers
void divide(float n1, float n2)
{
  float result = n1 / n2;
  display(n1, n2, '/', result);
}

// function for calculating remainder
void rem(float n1, float n2)
{
  //Modulus operator only works on int data type
  //Floating numbers are converted to int number
  int num1 = n1;
  int num2 = n2;
  int result = num1%num2;
  printf("%d %% %d = %d\n", num1, num2, result);
}

// function for calculating power
void power(float n1, float n2)
{
  if(n2<0) printf("Second number should be +ve.");
  else
  {
    float result=1.0;
    for(int i=1; i<=n2; i++)
    {
       result *= n1;
    }
    display(n1, n2, '^', result);
  }
}

Sample of Output:-

Enter two numbers: 20 10

*****************
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
Enter your choice: 1
20.00 + 10.00 = 30.00

*****************************************
Enter two numbers: 25 15

*****************
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
Enter your choice: 2
25.00 – 15.00 = 10.00

*****************************************
Enter two numbers: 15 4

*****************
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
Enter your choice: 5
15 % 4 = 3
*****************************************
Enter two numbers: 5 -3

*****************
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
Enter your choice: 6
Second number should be +ve.

The header file stdlib is used because we used the exit() function inside the switch case statement. When compiler encounters exit(0) then control came out from the program.

This program will keep executing until the user does not choose the exit option. Here, the do-while loop is used to execute the same logic multiple times.

For the remainder, we defined the function rem() which takes two arguments num1, and num2. The modulus operator doesn’t work on floating-point numbers. So, we should convert the floating-point number into an int data type. Now, We can use the modulus operator, but it returns the result in int. To get the result in the floating-point you can use fmod() function. The fmod() function returns floating-point value.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

3 thoughts on “Menu Driven Calculator Program in C Using Functions”

Leave a Comment

Your email address will not be published. Required fields are marked *