The arithmetic operators in C programming language are the operators in programming used to execute or complete arithmetic operations such as addition, subtraction, multiplication, division, modulus, and percentage. Arithmetic operators need two operands between one operator to perform all operations. In c programming there are 5 arithmetic operators, find the list of all operators below with examples.
For performing operations, we need two variables, for better understanding let’s take variables “first_number“, and “second_number” are two variables that help to perform all the operations(Addition, Subtraction, Multiplication, Division, and Modulus).
List of Arithmetic Operators in C
Arithmetic Operators | Meaning | Examples |
+ | Addition | 1008 + 108.8 = 1116.800000 |
– | Subtraction | 1008 – 108.8 = 899.200000 |
* | Multiplication | 1008 * 108.8 = 109670.400000 |
/ | Division | 1008 / 108.8 = 9.264706 |
% | Modulus | 1008 % 108.8 = 28.800000 |
We need 5 variables to store the result of two operands(first_number and second_number). The addition is denoted by the plus sign(+), Subtraction is denoted by the minus sign(-), Multiplication is denoted by the (*) Division is denoted by the(/), and Modulus is denoted by the Percent or Percentile sign(%) in programming languages.
Let takes an example of two numbers the first number is 1008 and the second number is 108.8. let’s perform all the arithmetic operators. To make this program universal so are using the Double variables to find all types of solutions in one program.
Addition
To solve this problem we need 3 variables, let’s take variables. for testing purposes, we are taking two numbers to test our programming code solution on arithmetic operations. This is a very simple exercise compared to an anagram.
float first_number, second_number, addition;
addition = first_number + second_number;
Now time to print the result.
printf("Sum of first_number and second_number are = %lf\n", addition);
The output of Modulus of Two Numbers is = 1116.800000
Subtraction
The process is the same for the Subtraction, only the operator and variable name need to change.
float first_number, second_number, subtraction;
subtraction = first_number - second_number;
Print the result.
printf("Subtraction of first_number and second_number are = %lf\n", subtraction);
The output of Modulus of Two Numbers are = 899.200000
Multiplication & Division
Repeat the same process for multiplication and Division.
The output of Modulus of Two Numbers are = 109670.400000
The output of Modulus of Two Numbers are = 9.264706
Modulus
This is a little bit different cause in this operation we do not calculate Quotients, we calculate the Remainder. It is tricky as well we cannot perform a Float or Double value directly, so we are using the predefined functions in C and for that, we need to add one extra header file math.h.
modulus = fmod(first_number, second_number);
Here fmod is a predefined function in C and C++ language, Now it’s time to print the output of the program.
printf("Modulus of Two Numbers are = %lf", modulus);
The output of Modulus of Two Numbers are = 28.800000
Required Skills: for solving this programming problem you need to know Format Specifiers, Operators, Input/Output, Escape Sequence, and Data Types.
This program is tested and runs successfully on Dev-C++ and some other popular programming editors.
Arithmetic Operators Program in C
/*
Write a C program to enter two numbers and perform all arithmetic operations.
*/
#include <stdio.h>
#include <math.h>
int main() {
double first_number, second_number;
double addition, subtraction, multiplication, division, modulus;
/* Two numbers input from user */
printf("Enter the First Numbers to Perform Arithmetic Operations: ");
scanf("%lf", & first_number);
printf("Enter the Second Numbers to Perform Arithmetic Operations: ");
scanf("%lf", & second_number);
/* Performing an arithmetic operations(addition, subtraction, * multiplication, division, and modulus) */
addition = first_number + second_number;
subtraction = first_number - second_number;
multiplication = first_number * second_number;
division = first_number / second_number;
modulus = fmod(first_number, second_number);
/* Printing the result of all arithmetic operations */
printf("\n\nSum of Two Numbers are = \t\t%lf\n", addition);
printf("Differences of Two Numbers are = \t%lf\n", subtraction);
printf("Multiplication of Two Numbers are = \t%lf\n", multiplication);
printf("Quotients of Two Numbers are = \t\t%lf\n", division);
printf("Modulus of Two Numbers are = \t\t%lf\n\n", modulus);
return 0;
}
Arithmetic Operators Output
Similar Programming Exercises and Solutions
- Input/Output of All Basic Data Types.
- Print 1 to 100 Numbers
- Greatest of Three Numbers
- Print Even or Odd
- Area of Triangle – Mathematics based
- LCM and GCD of Two Numbers
- Find the Area of an Equilateral Triangle
- Print All the Odd Number Till ‘N’
- Swap Two Numbers Using 3rd Variable
- Swapping Two Values Without Using 3rd Variable