Arithmetic Operators in C – {Add, Subtract, Multiply, Divide, and Modulus}

2
231
List of arithmetic operators

The arithmetic operators in C programming language are the operators in programming used to execute or complete the 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 helps to perform all the operations(Addition, Subtraction, Multiplication, Division, and Modulus).

List of Arithmetic Operators

Arithmetic OperatorsMeaningExamples
+ Addition1008 + 108.8 = 1116.800000
Subtraction1008 – 108.8 = 899.200000
*Multiplication1008 * 108.8 = 109670.400000
/Division1008 / 108.8 = 9.264706
%Modulus1008 % 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 (*) and 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 type of solution in one program.

Addition: To solve this problem we need 3 variables, let’s take variables. for testing purpose, we are taking two numbers for 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 times 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 operator and variable name need to change the.

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 the 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 operations we do not calculates Quotients, we calculate 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 have a knowledge of Format Specifiers, Operators, Input/Output, Escape Sequence, and Data Types.

This program is tested and run 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; 
}

Output

arithmetic operations Solution 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 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

2 COMMENTS

  1. Hi there, I found your web site by way of Google whilst searching for a similar topic,
    your website got here up, it appears great. I have bookmarked it in my google bookmarks.

    Hello there, simply changed into alert to your weblog via Google,
    and found that it is really informative. I’m gonna be careful for brussels.
    I will be grateful should you continue this in future.
    Many folks can be benefited out of your writing.
    Cheers!

LEAVE A REPLY

Please enter your comment!
Please enter your name here