- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
C program to perform addition, subtraction, multiplication, and division
In this article, we will learn about how to create a program that works on the famous four mathematical operations such as add, subtract, multiply, and divide in the following ways:
- Mathematical operation on integer data only
- Mathematical operation on real data or a floating-point value
- Mathematical operation using a user-defined function
- Mathematical operation based on the user's choice
Add, Subtract, Multiply, and Divide in C
The question is: Write a program in C that performs all four basic mathematical operations, such as addition, subtraction, multiplication, and division.
Here is the program in C that solves the above question, but it is only applicable for integer data.
#include<stdio.h> #include<conio.h> int main() { int num1, num2, res; printf("Enter any two number: "); scanf("%d%d", &num1, &num2); res = num1+num2; printf("\nAddition = %d", res); res = num1-num2; printf("\nSubtraction = %d", res); res = num1*num2; printf("\nMultiplication = %d", res); res = num1/num2; printf("\nDivision = %d", res); getch(); return 0; }
The above program was written in the Code::Blocks IDE; therefore, after a successful compile and run, here is the initial output:
Now supply any two numbers, say 90 and 45, as input and press the ENTER key to see the following output:
Main steps used in the above program
Here are some of the main steps used in the above program:
- Initialize three variables of integer type, say num1, num2, and res.
- Variables num1 and num2 will be used to hold the value of the first and second numbers entered by the user at run-time. And the variable res will be used to store the value after performing the operation, such as add, subtract, multiply, and divide between two numbers.
- Receive the input from the user.
- Now initialize the addition result of the given two numbers stored in variables num1 and num2, respectively, to the variable res.
- Print the value of res as the sum of the two numbers given.
- Do the same for subtraction, multiplication, and division operations.
Program to operate on real numbers
Now what if the user supplies any input number that is not a pure integer? Let's suppose that the user supplied any input, say 9.0 and 4.5, then the above program will not produce the correct result, as shown in the output given below:
To solve this problem, here is the modified version of the above program:
#include<stdio.h> #include<conio.h> int main() { float num1, num2, res; printf("Enter any two number: "); scanf("%f%f", &num1, &num2); res = num1+num2; printf("\nAddition = %.2f", res); res = num1-num2; printf("\nSubtraction = %.2f", res); res = num1*num2; printf("\nMultiplication = %.2f", res); res = num1/num2; printf("\nDivision = %.2f", res); getch(); return 0; }
Here, we have changed the data type to "float" to handle real data or any real number that may include a decimal or not. After running the above program, enter any two numbers, say 9.0 and 4.5, and press the ENTER key to see the following output:
To receive any floating-point (real number) data, use %f as a format specifier. To print the value of the res variable, we have placed. 2 between the format specifiers, say % and f, to print only two digits after the third decimal.
Mathematical Operation Program in C using User-defined Function
Here is the program that works the same as the above program does, but internally, this program is programmed using user-defined functions. Let's take a look at it:
#include<stdio.h> #include<conio.h> float Add(float, float); float Sub(float, float); float Mul(float, float); float Div(float, float); int main() { float num1, num2, res; printf("Enter any two number: "); scanf("%f%f", &num1, &num2); res = Add(num1, num2); printf("\nAddition = %.2f", res); res = Sub(num1, num2); printf("\nSubtraction = %.2f", res); res = Mul(num1, num2); printf("\nMultiplication = %.2f", res); res = Div(num1, num2); printf("\nDivision = %.2f", res); getch(); return 0; } float Add(float a, float b) { return a+b; } float Sub(float a, float b) { return a-b; } float Mul(float a, float b) { return a*b; } float Div(float a, float b) { return a/b; }
If you run the above program, it will work the same as the previous program in this article.
Mathematical operation based on the user's choice
Now here is another version of almost the same program as previous programs, but this time, the program will not automatically operate and print the result of all four mathematical operations on the output screen.
Rather, this program will ask the user what to do, either to find addition or any other three mathematical operations such as subtraction, multiplication, and division. Let's take a look at it:
#include<stdio.h> #include<conio.h> int main() { float num1, num2; char ch; printf("Enter First Number: "); scanf("%f", &num1); printf("Enter Second Number: "); scanf("%f", &num2); printf("Enter the Operator (+, -, *, /): "); scanf(" %c", &ch); if(ch=='+') printf("Result = %0.2f", num1+num2); else if(ch=='-') printf("Result = %0.2f", num1-num2); else if(ch=='*') printf("Result = %0.2f", num1*num2); else if(ch=='/') printf("Result = %0.2f", num1/num2); else printf("Wrong Operator"); getch(); return 0; }
When the above C program is compiled and executed, it will produce the following result:
Provide any input, say 45 as the first number and 14 as the second number, and press / as the division operator, then press the ENTER key to see the output as shown in the snapshot given below:
The same program in different languages
- C++ Add, Subtract, Multiply and Divide
- Java Add, Subtract, Multiply and Divide
- Python Add, Subtract, Multiply and Divide
« Previous Program Next Program »