Write a program to enter two numbers and perform all arithmetic operations in C

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 #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:

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Write a C program to input two numbers and perform all arithmetic operations. How to perform all arithmetic operation between two numbers in C programming. C program to find sum, difference, product, quotient and modulus of two given numbers.

Example
InputFirst number: 10 Second number: 5

Output

Sum = 15 Difference = 5 Product = 50 Quotient = 2 Modulus = 0

Required knowledge

Arithmetic operators, Data types, Basic Input/Output

In previous post I explained to find the sum of two numbers.

Read more - Program to find sum of two numbers

In this exercise, we will pedal bit more and compute results of all arithmetic operations at once.

Program to perform all arithmetic operations

/** * C program to perform all arithmetic operations */ #include <stdio.h> int main() { int num1, num2; int sum, sub, mult, mod; float div; /* * Input two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); /* * Perform all arithmetic operations */ sum = num1 + num2; sub = num1 - num2; mult = num1 * num2; div = (float)num1 / num2; mod = num1 % num2; /* * Print result of all arithmetic operations */ printf("SUM = %d\n", sum); printf("DIFFERENCE = %d\n", sub); printf("PRODUCT = %d\n", mult); printf("QUOTIENT = %f\n", div); printf("MODULUS = %d", mod); return 0; }

In statement div = (float) num1 / num2;, I have typecasted num1 to float before the divide operation, to avoid integer division.

Read more - Type conversion in C programming.

Note: \n is an escape sequence character used to print new lines (move to the next line).

Enter any two numbers : 20 10 SUM = 30 DIFFERENCE = 10 PRODUCT = 200 QUOTIENT = 2.000000 MODULUS = 0

Happy coding 😉

Write a C program to input two numbers and perform all arithmetic operations. How to perform all arithmetic operation between two numbers in C programming. C program to find sum, difference, product, quotient and modulus of two given numbers.

Required knowledge
Data Types in c, Input/Output in C, C Operators

In this exercise, we will pedal bit more and compute results of all arithmetic operations at once.

Program to perform all arithmetic operations

/** * C program to perform all arithmetic operations */ #include <stdio.h> int main() { int num1, num2; int sum, sub, mult, mod; float div; /* * Input two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); /* * Perform all arithmetic operations */ sum = num1 + num2; sub = num1 - num2; mult = num1 * num2; div = (float)num1 / num2; mod = num1 % num2; /* * Print result of all arithmetic operations */ printf("SUM = %d\n", sum); printf("DIFFERENCE = %d\n", sub); printf("PRODUCT = %d\n", mult); printf("QUOTIENT = %f\n", div); printf("MODULUS = %d", mod); return 0; }

In statement div = (float) num1 / num2;, I have typecasted num1 to float before the divide operation, to avoid integer division.

Note: \n is an escape sequence character used to print new lines (move to the next line).

Output:

Enter any two numbers : 20 10 SUM = 30 DIFFERENCE = 10 PRODUCT = 200 QUOTIENT = 2.000000 MODULUS = 0



Page 2

Computers Languages

         C++            Java

Work from Home

.

Category: C Program • Tags: cprogram operator

Write a C program to enter two numbers and perform all arithmetic operations like addition, subtraction etc to understand how to perform basic arithmetic operations in C language.

In this program we are going to perform all basic operations like addition, subtraction, multiplication, division, modulus and finding quotient of two numbers.

Input: Enter first number: 20 Enter second number: 10 Output: Sum = 30 Difference = 10 Multiply = 200 Modulus = 0 Quotient = 2

#include <stdio.h> void main() { int num1, num2; int sum, diff, mul, div, mod, quo; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); sum = num1 + num2; diff = num1 - num2; mul = num1 * num2; mod = num1 % num2; quo = num1 / num2; printf("Sum = %d\n", sum); printf("Difference = %d\n", diff); printf("Multiply = %d\n", mul); printf("Modulus = %d\n", mod); printf("Quotient = %d\n", quo); }

Output

Output

Enter first number: 20 Enter second number: 10 Sum = 30 Difference = 10 Multiply = 200 Modulus = 0 Quotient = 2