C Program for Addition Subtraction Multiplication Division using Function

Here we will write a C program for addition subtraction multiplication and division using the function. First, we will create a simple program to solve the program, then we will write the same program where input is taken and the result is displayed using functions. At last, we will develop a menu-driven program, where the user has a choice, which operation he/she wants to perform addition or subtraction or multiplication or division. Previously we had already developed a program for addition, subtraction, multiplication, and division operation but without using the function.

C Program for Addition Subtraction Multiplication and Division using the Function

Program description:- C program for addition subtraction multiplication and division using the function.

#include<stdio.h>

// functions declaration
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);

// main function
int main()
{
  int num1, num2;

  printf("Enter two numbers: ");
  scanf("%d %d", &num1, &num2);

  printf("%d + %d = %d\n", num1, num2, add(num1, num2));
  printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
  printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
  printf("%d / %d = %d\n", num1, num2, divide(num1, num2));

  return 0;
}

// function to add two integer numbers
int add(int n1, int n2)
{
  int result;
  result = n1 + n2;
  return result;
}

// function to subtract two integer numbers
int subtract(int n1, int n2)
{
  int result;
  result = n1 - n2;
  return result;
}

// function to multiply two integer numbers
int multiply(int n1, int n2)
{
  int result;
  result = n1 * n2;
  return result;
}

// function to divide two integer numbers
int divide(int n1, int n2)
{
  int result;
  result = n1 / n2;
  return result;
}

Output for the different test-cases:-

Enter two numbers: 20 5
20 + 5 = 25
20 – 5 = 15
20 * 5 = 100
20 / 5 = 4

In this program we have taken two integer numbers, you can also take floating-point numbers in your own program. But in that case inside every function replace the int keyword with a float or double.

Program2

Program:- Write a C program for addition subtraction multiplication and division. Perform every operation through function. Take input inside a user-defined function, and also display the result from another user-defined function.

#include<stdio.h>

// function declarations
int input();
void display(int n1, int n2, char ch, int result);
void add(int n1, int n2);
void subtract(int n1, int n2);
void multiply(int n1, int n2);
void divide(int n1, int n2);

// main function
int main()
{
  int n1, n2;
  n1 = input();
  n2 = input();

  add(n1, n2);
  subtract(n1, n2);
  multiply(n1, n2);
  divide(n1, n2);
  return 0;
}

// function for taking input from user
int input()
{
  int n;
  printf("Enter number: ");
  scanf("%d",&n);
  return n;
}

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

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

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

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

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

Output:-

Enter number: 19
Enter number: 9
19 + 9 = 28
19 – 9 = 10
19 * 9 = 171
19 / 9 = 2

Menu-driven C Program for Addition Subtraction Multiplication and Division using Function

Program description:- Write a menu-driven program to find addition, subtraction, multiplication, and division of two numbers using the user defined functions and the program should accept choice from the user repeatedly.

#include<stdio.h>

// function for taking input from user
float input()
{
  float n;
  scanf("%f",&n);
  return n;
}

// 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(int n1, float n2)
{
  float result;
  result = n1 + n2;
  display(n1, n2, '+', result);
}

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

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

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

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

  // do-while loop
  do
  {
    printf("Enter first number: ");
    n1 = input();
    printf("Enter second number: ");
    n2 = input();
     
    printf("\nWhich operation you want to perform,\n");
    printf("1.Addition\n");
    printf("2.Subtraction\n");
    printf("3.Multiplication\n");
    printf("4.Division\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
     
    switch (choice) {
      case 1:
        add(n1, n2);
        break;
      case 2:
        subtract(n1, n2);
        break;
      case 3:
        multiply(n1, n2);
        break;
      case 4:
        divide(n1, n2);
        break;
      default:
        printf("Invalid choice");
    }

    printf("\nDo you want to continue (y/n): ");
    scanf("%c",&ch);
    scanf("%c",&ch);
    printf("***************************************\n");

  } while(ch=='y');

  printf("\nThank You.");
  return 0;
}

Output Samples:-

Enter first number: 10
Enter second number: 5

Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 1
10.00 + 5.00 = 15.00

Do you want to continue (y/n): y
***************************************
Enter first number: 15
Enter second number: 10

Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 2
15.00 – 10.00 = 5.00

Do you want to continue (y/n): y
***************************************
Enter first number: 5
Enter second number: 2

Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 3
5.00 * 2.00 = 10.00

Do you want to continue (y/n): y
***************************************
Enter first number: 40
Enter second number: 8

Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 4
40.00 / 8.00 = 5.00

Do you want to continue (y/n): n
***************************************

Thank You.

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!

1 thought on “C Program for Addition Subtraction Multiplication Division using Function”

  1. Franklyn Dicharry

    Hey there, You have performed a great job. I’ll certainly digg it and for my part suggest to my friends. I am confident they’ll be benefited from this web site.

Leave a Comment

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