Home » Calculator program in C using functions

Calculator program in C using functions

Learn about Calculator program in C using functions in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Calculator program in C using functions

Program:

#include<stdio.h>
#include<math.h>

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);


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;
}


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


void add(float n1, float n2)
{
  float result = n1 + n2;
  display(n1, n2, '+', result);
}


void subtract(float n1, float n2)
{
  float result = n1 - n2;
  display(n1, n2, '-', result);
}


void multiply(float n1, float n2)
{
  float result = n1 * n2;
  display(n1, n2, '*', result);
}

void divide(float n1, float n2)
{
  float result = n1 / n2;
  display(n1, n2, '/', result);
}


void rem(float n1, float n2)
{

  int num1 = n1;
  int num2 = n2;
  int result = num1%num2;
  printf("%d %% %d = %d\n", num1, num2, result);
}


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);
  }
}

Output:

Enter two numbers: 8 2
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
Enter your choice: 2
8.00 – 2.00 = 6.00

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


Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Menu-driven C Program for Addition Subtraction Multiplication and Division using Function
C Program for Addition Subtraction Multiplication and Division using the userdefined Function