- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- 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 Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest 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 Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- 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 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array 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 Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C More Programs
- C Check Palindrome String
- C Inches to Centimetres
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

In this article, we will learn about how to create a program that works on famous four mathematical operation such as add, subtract, multiply and divide in following ways:

- Mathematical operation on integer data only
- Mathematical operation on real data or floating-point value
- Mathematical operation using user-defined function
- Mathematical operation based on user's choice

The question is *Write a program in C that performs all the four basic mathematical operation such as addition, subtraction,
multiplication and division*.

Here is the program in C that solves the above question, but only applicable for integer data.

// -----------codescracker.com-------------- #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 (1); }

The above program was written under **Code::Blocks** IDE, therefore after successful compile and run, here is the initial output:

Now supply any two number say **90** and **45** as input and press **ENTER** key to see the following output:

Here are some of the main steps used in 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 first number and second number entered by 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 given two number - Receive the input from user
- Now initialize the addition result of given two number stored in variables
**num1**and**num2**respectively, to the variable**res** - Print the value of
**res**as Addition result of given two number - Do the same for subtraction, multiplication, and division operation

Now what if user supply any input number that is not a pure integer. Let's suppose that 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:

// ----------codescracker.com-------------- #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 (1); }

Here we have changed the data type to **float** to handle real data or any real number that may include decimal or not.
After running the above program, enter any two number say **9.0** and **4.5** and press **ENTER** key to see the following
output:

To receive any floating-point (real number data), use **%f** as format specifier. Here to print the value of **res** variable,
we have placed **.2** between the format specifier say **%** and **f**, to print only two digit after decimal.

Here is the program that works same as above program does, but internally this program is programmed using user-defined function. Let's take a look at it:

// ----------codescracker.com-------------- #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 (1); } 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 will run the above program, this works same as previous program works in this article. To learn more about function, refer to Function in C.

Now here is another version of almost same program as previous programs are, but this time, the program will not automatically operate and print the result of all four mathematical operation on output screen.

Rather, this program will ask from user, what to do, either to find addition or any other three mathematical operation such as subtraction, multiplication, and division. Let's take a look at it:

// Program to Perform Operation based on User's Choice // ----------codescracker.com----------- #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 compile and executed, it will produce the following result:

Provide any input say **45** as first number and **14** as second number and press **/** as (division operator), then
press **ENTER** key to see the output as shown in scnapshot given below:

To learn more about **if** or **else if** statement, refer to if-else in C.

- C++ Add, Subtract, Multiply and Divide
- Java Add, Subtract, Multiply and Divide
- Python Add, Subtract, Multiply and Divide

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.