Arithmetic Operators in C

In this blog, we will see the Arithmetic operator in C. In the C programming language, operators are special symbols that are used to carry out different mathematical and logical operations on the specified operands and produce the desired outputs. The C programming language supports a number of operators, including assignment operators, ternary or conditional operators, shift operators, logical operators, bitwise operators, and relational operators. However, we shall just comprehend the arithmetic operators in C programming language here.

Arithmetic Operators
Mathematical operations like addition, subtraction, multiplication, division, modulus, etc. are performed on the provided operands using an arithmetic operator.

Some examples of arithmetic operators are 5 + 3 = 8, 5 – 3 = 2, 2 * 4 = 8, etc.

Arithmetic Operators used in C language

Let’s discuss the different types of Arithmetic Operators in C programming.

Plus Operator

To add two specified operands, use the straightforward plus (+) operator. To add the specified operand, we may use the plus operator with a variety of data types, including integer, float, long, double, enumerated, and text data.

Syntax

C = A + B;

For instance, we need to get the total of the two operands, 5, and 6, which are given. As a result, we add 11 using the "+" operator between the given values.

Code Implementation:

#include <stdio.h>  
int main ()  
{  
    int num1, num2, res;   
    // declare float data type number  
    float f1, f2, res1;  
    // declare double variables  
    double d1, d2, res2;  
    printf (" Enter two integer numbers: ");  
    scanf ("%d %d", &num1, &num2);  
    res = num1 + num2; // use + operator  
      
    printf (" Enter two float numbers: \n ");  
    scanf ("%f %f", &f1, &f2);  
    res1 = f1 + f2; // use + operator  
      
    printf (" Enter two double data type numbers: \n ");  
    scanf ("%lf %lf", &d1, &d2);  
    res2 = d1 + d2; // use + operator  
    printf (" The sum of two integer numbers: %d \n", res);  
    printf (" The sum of two float numbers: %f \n ", res1);  
    printf (" The sum of two double numbers: %lf", res2);  
    return 0;  
}

Minus Operator

The minus (-) sign designates the minus operator. It is utilized to retrieve the result of subtracting the first integer from the second. The programming language allows for a variety of data types for the given integer, including int, float, double, long double, etc.

Syntax

C = A - B;

For illustration, there are two operands, 15 and 6, and we want their subtraction result. So, we use the ‘-‘ Operator between the given numbers that return data 9.

Code Implementation:

#include <stdio.h>   
int main ()  
{  
    // declare integer variables  
    int num1, num2, res;   
    // declare float data type number  
    float f1, f2, res1;  
    // declare double variables  
    double d1, d2, res2;  
    printf (" Enter two integer numbers: ");  
    scanf ("%d %d", &num1, &num2);  
    res = num1 - num2; // use + operator  
      
    printf (" Enter two float numbers: \n ");  
    scanf ("%f %f", &f1, &f2);  
    res1 = f1 - f2; // use + operator  
      
    printf (" Enter two double data type numbers: \n ");  
    scanf ("%lf %lf", &d1, &d2);  
    res2 = d1 - d2; // use + operator  
    printf (" The subtraction of two integer numbers: %d \n", res);  
    printf (" The subtraction of two float numbers: %f \n ", res1);  
    printf (" The subtraction of two double numbers: %lf", res2);  
    return 0;  
}

Multiplication Operator

The star (*) symbol, which is used to denote the multiplication operator, returns the product of the input values a1 and a2. In the C programming language, the data type of the provided integer can be any type, including int, float, and double.

Syntax

C = A * B;

For instance, we want to get the output of the two operands 15 and 6. Therefore, between the given integers that produce the int data 90, we may utilize the ‘*’ Operator.

Code Implementation:

#include <stdio.h>  
int main ()  
{  
    // declare integer variables  
    int num1, num2, res;   
    // declare float data type number  
    float f1, f2, res1;  
    // declare double variables  
    double d1, d2, res2;  
    printf (" Enter two integer numbers: ");  
    scanf ("%d %d", &num1, &num2);  
    res = num1 * num2; // use * operator  
      
    printf (" Enter two float numbers: \n ");  
    scanf ("%f %f", &f1, &f2);  
    res1 = f1 * f2; // use * operator  
      
    printf (" Enter two double data type numbers: \n ");  
    scanf ("%lf %lf", &d1, &d2);  
    res2 = d1 * d2; // use * operator  
    printf (" The multiplication of two integer numbers: %d \n", res);  
    printf (" The multiplication of two float numbers: %f \n ", res1);  
    printf (" The multiplication of two double numbers: %lf", res2);  
    return 0;  
}

Division Operator

The division operator divides the first number (n1) by the second number (n2) using arithmetic operations. We may divide variables of the int, float, double, and long data types with the division operator (/).

Syntax

C = A / B;

For example, we want to determine the division result of two operands, 25.5 and 5.0. So, between the provided numbers that produce the float data 5.1, we use the operator "/".

Code Implementation:

#include <stdio.h>  
int main ()  
{  
    // declare integer variables  
    int num1, num2, res;   
    // declare float data type number  
    float f1, f2, res1;  
    // declare double variables  
    double d1, d2, res2;  
    printf (" Enter two integer numbers: ");  
    scanf ("%d %d", &num1, &num2);  
    res = num1 / num2; // use / operator  
      
    printf (" Enter two float numbers: \n ");  
    scanf ("%f %f", &f1, &f2);  
    res1 = f1 / f2; // use / operator  
      
    printf (" Enter two double data type numbers: \n ");  
    scanf ("%lf %lf", &d1, &d2);  
    res2 = d1 / d2; // use / operator  
    printf (" The division of two integer numbers: %d \n", res);  
    printf (" The division of two float numbers: %f \n ", res1);  
    printf (" The division of two double numbers: %lf", res2);  
    return 0;  
}

Modulus Operator

The remainder is returned by dividing the first number by the second number using the modulus operator, which is denoted by the percentage sign (%).

Syntax

C = A % B;

For illustration, we want to get the modulus of the two operands 25 and 4. Therefore, we insert the "%" operator between the given values, which returns the remainder as 1.

Code Implementation:

#include <stdio.h>  
int main ()  
{  
    // declare integer variables  
    int num1, num2, res;   
    printf (" Enter two integer numbers: ");  
    scanf ("%d %d", &num1, &num2);  
    res = num1 % num2; // use % operator  
    printf (" The modulus of two integer numbers: %d \n", res);  
    return 0;  
}

Increment Operator

The double plus (++) operator is used to indicate the sort of arithmetic operators in C known as an increment operator. It is applied to add one to the integer value.

Syntax

B = A++;

Assuming A has an integer value of 10, for example, the increment operator increases the operand value by one, returning 11.

Code Implementation:

#include <stdio.h>  
int main ()  
{  
    // declare integer variables  
    int num1, res;   
      
    printf (" Enter the positive number: ");  
    scanf ("%d", &num1);  
    res = num1++; // use increment operator  
      
    printf (" The incremented value is: %d ", num1);  
    return 0;  
}

Decrement Operator

The double minus (–) sign, which reduces the operand value by 1, is used to indicate the decrement operator.

Syntax

B = A--;

Assuming A has an integer value of 10, for example, the decrement operator reduces the operand value by 1 and returns 9.

Code Implementation:

#include <stdio.h>  
int main ()  
{  
    // declare integer variables  
    int num1, res;   
      
    printf (" Enter the positive number: ");  
    scanf ("%d", &num1);  
    res = num1--; // use decrement operator  
      
    printf (" The decremented value is: %d ", num1);  
    return 0;  
}

Conclusion:
In this article, we have taught Arithmetic operator in C, and we have illustrated almost all the Operators in C with proper examples. The article starts with a brief introduction to Operators in C followed by elaborating on the various types of Arithmetic operator in C.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers
Calculator Program in C Language
Simple Interest Program in C
Compound Interest Program in C
Priority Scheduling Program in C
Doubly Linked List Program in C
FCFS Scheduling Program in C
Insertion Sort Program in C
Singly Linked List Program in C
Star Program in C
String Program in C
Radix Sort Program in C

Leave a Reply

Your email address will not be published. Required fields are marked *