c program for addition subtraction, multiplication and division using function

  1. #include <stdio.h>

  2. int add(int a, int b) {
  3.     return a + b;
  4. }

  5. int subtract(int a, int b) {
  6.     return a - b;
  7. }

  8. int multiply(int a, int b) {
  9.     return a * b;
  10. }

  11. float divide(int a, int b) {
  12.     return (float)a / b;
  13. }

  14. int main() {
  15.     int a = 10, b = 5;
  16.     int add_result = add(a, b);
  17.     int subtract_result = subtract(a, b);
  18.     int multiply_result = multiply(a, b);
  19.     float divide_result = divide(a, b);

  20.     printf("Addition: %d + %d = %d\n", a, b, add_result);
  21.     printf("Subtraction: %d - %d = %d\n", a, b, subtract_result);
  22.     printf("Multiplication: %d * %d = %d\n", a, b, multiply_result);
  23.     printf("Division: %d / %d = %f\n", a, b, divide_result);

  24.     return 0;
  25. }


  • In this program, four functions are defined for addition, subtraction, multiplication, and division respectively. Each function takes two integer arguments a and b, performs the corresponding operation, and returns the result. 
  • In the main function, variables a and b are initialized with the values 10 and 5 respectively.
  • The functions are called and the results are stored in variables add_result, subtract_result, multiply_result, and divide_result. 
  • These functions take two integer arguments and return the result of the operation. In the main() function, two integers are taken as input, and then the functions are called with these inputs to perform the operations and display the results.
  • In the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. 
  • It's also important to consider the case where the division operation results in decimal values, In that case, you should use float or double instead of int.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu