C Program to Perform Arithmetic Operations Using Functions

In this program, we will learn how to perform arithmetic operations using functions in the C Programming language.

This program asks the user to enter two numbers. Then, it finds Addition, Subtraction, Multiplication, Division and Modulus of those two numbers using user-defined functions.

So, without further ado, let’s begin this tutorial.

C Program to Perform Arithmetic Operations Using Functions

// C Program to Perform Arithmetic Operations Using Functions
#include <stdio.h>

// Declaring addition function
int addition(int a, int b){
    int sum = a + b;
    return sum;
}

// Declaring subtraction function
int subtract(int a, int b){
    int difference = a - b;
    return difference;
}

// Declaring multiplication function
int multiply(int a, int b){
    int multiply = a * b;
    return multiply;
}

// Declaring division function
float division(float a, float b){
    float divide = a / b;
    return divide;
}

// Declaring modulus function
float mod(int a, int b){
    int rem = a % b;
    return rem;
}

int main(){
    int num1, num2;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    
    // Displaying output
    printf("Arithmetic operations on %d and %d: \n", num1, num2);
    printf("Addition: %d\n", addition(num1, num2));
    printf("Subtraction: %d\n", subtract(num1, num2));
    printf("Multiplication: %d\n", multiply(num1, num2));
    printf("Division: %f\n", division(num1, num2));
    printf("Modulus: %d\n", mod(num1, num2));
    return 0;
}

Output

Enter the first number: 8
Enter the second number: 3
Arithmetic operations on 8 and 3: 
Addition: 11
Subtraction: 5
Multiplication: 24
Division: 2.666667
Modulus: 3

How Does This Program Work ?

// Declaring addition function
int addition(int a, int b){
    int sum = a + b;
    return sum;
}

In this program, we have defined a custom function named addition which returns the sum of two numbers.

// Declaring subtraction function
int subtract(int a, int b){
    int difference = a - b;
    return difference;

Similarly, we defined a function named subtract which returns the difference of two numbers.

// Declaring multiplication function
int multiply(int a, int b){
    int multiply = a * b;
    return multiply;
}

Now, we defined a custom function named multiply which returns the product of two numbers.

// Declaring division function
float division(float a, float b){
    float divide = a / b;
    return divide;
}

We defined a custom function named division which performs division of two numbers.

// Declaring modulus function
float mod(int a, int b){
    int rem = a % b;
    return rem;
}

At last, we defined a custom function named mod which returns the modulus of two numbers.

    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);

Now, the user is asked to enter two numbers.

    // Displaying output
    printf("Arithmetic operations on %d and %d: \n", num1, num2);
    printf("Addition: %d\n", addition(num1, num2));
    printf("Subtraction: %d\n", subtract(num1, num2));
    printf("Multiplication: %d\n", multiply(num1, num2));
    printf("Division: %f\n", division(num1, num2));
    printf("Modulus: %d\n", mod(num1, num2));

Finally, we call all the custom functions in the main function and display the result with the help of printf() function.

Conclusion

I hope after going through this post, you understand how to perform arithmetic operations using functions in C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

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