Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Arithmetic Operators in C

Last Updated on May 22, 2023 by Prepbytes

Arithmetic operators are an essential component of the C programming language, enabling developers to perform mathematical calculations on numerical values. C provides a set of built-in arithmetic operators that allow you to perform addition, subtraction, multiplication, division, and more. These operators are fundamental for implementing mathematical algorithms, performing calculations, and manipulating numerical data within C programs.

Arithmetic Operators

An arithmetic operator is used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, etc. on the specified operands.

Mathematical operators include things like 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

Use the simple addition (+) operator to combine two given operands. The plus operator can be used with a variety of data types, including integer, float, long, double, enumerated, and text data, to add the specified operand.

Syntax

C = A + B;

For instance, we must find the sum of the two supplied operands, 5, and 6. As a result, we use the "+" operator between the supplied integers to add 11.

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 negative operator is indicated by the symbol (-). In order to get the outcome of dividing the first integer by the second, it is used. For the given integer, the programming language supports a number of data types, including int, float, double, long double, etc.

Syntax

C = A - B;

Two operands, 15 and 6, are used as an example, and we want the result of their subtraction. Therefore, between the supplied integers that return data 9, we employ 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 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 input values a1 and a2 are multiplied together when the multiplication operator is indicated by the star (*). The data type of the supplied integer can be any type in the C programming language, including int, float, and double.

Syntax

C = A * B;

As an illustration, we desire the results of the two operands, 15, and 6. As a result, we may use the ‘*’ Operator between the given numbers to obtain the int data 90.

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 applies arithmetic operations to divide the first number (n1) by the second number (n2). The division operator (/) can be used to divide variables of the int, float, double, and long data types.

Syntax

C = A / B;

For instance, we need to know the outcome of the division of two operands, 25.5 and 5.0. Therefore, we employ the operator "/" between the supplied values to get the float data 5.1.

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 modulus operator, represented by the percentage sign (%), is used to divide the first value by the second integer and return the remainder.

Syntax

C = A % B;

We want to know the modulus of the two operands, 25 and 4, as an example. In order to return the remainder as 1, we add the "%" operator between the supplied integers.

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

In C, the type of arithmetic operators known as an increment operator are denoted by the double plus (++) operator. The integer value is increased by one using it.

Syntax

B = A++;

The increment operator increases the operand value by one, or 11, if A, for instance, has an integer value of 10.

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 decrement operator is denoted by the double minus (-) sign, which decreases the operand value by 1.

Syntax

B = A--;

For instance, if A is an integer with a value of 10, the decrement operator would subtract 1 from the operand value and return 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:
Arithmetic operators play a vital role in C programming, providing the means to perform mathematical calculations and manipulate numerical data. With operators like addition, subtraction, multiplication, division, and modulo, developers can implement a wide range of numerical algorithms and solve complex mathematical problems efficiently.

By leveraging arithmetic operators, programmers can perform calculations on variables, constants, and expressions, enabling them to handle numeric data effectively. These operators are not only fundamental for basic arithmetic operations but also essential for advanced tasks such as data analysis, scientific computations, financial calculations, and game development.

FAQ related to Arithmetic Operators in C

Q1. Can arithmetic operators be used with variables in C?
Ans. Yes, arithmetic operators can be used with variables in C. For example, you can add, subtract, multiply, divide, or calculate the remainder using variables.

Q2. What is the precedence of arithmetic operators in C?
Ans. In C, arithmetic operators follow the precedence rules. The order of precedence from highest to lowest is parentheses, multiplication, division, modulo, addition, and subtraction. However, you can use parentheses to explicitly define the order of operations.

Q3. Can I perform mixed-type arithmetic operations in C?
Ans. Yes, C supports mixed-type arithmetic operations. When you perform arithmetic operations on different data types, the C compiler automatically promotes the operands to a common type based on the rules of type conversion.

Q4. What happens if I divide an integer by zero using the division operator?
Ans. Division by zero is undefined in C. If you attempt to divide an integer by zero, the program may result in an error or produce unexpected behavior. It is important to handle such cases with proper error checking and validation.

Q5. Are there any shorthand notations for arithmetic operations in C?
Ans. Yes, C provides compound assignment operators that combine arithmetic operations with assignment in a single expression. For example, += adds and assigns a value, -= subtracts and assigns a value, *= multiplies and assigns a value, and so on.

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 *