C Arithmetic Operators
C Arithmetic Operators
In this tutorial, we will learn about different C Arithmetic Operators with some examples. The arithmetic operators are as follows in table format with operator and its description:
Arithmetic Operation | Arithmetic Operator | Description | Examples |
---|---|---|---|
Addition | + | The addition operator adds the two operands | sum = a + b |
Subtraction | – | Subtracts the second operand from the first operand | diff = a – b |
Multiplication | * | The multiplication operator multiplies the two operands | product = a * b |
Division | / | The division operator divides the numerator by the denominator | div = a/b |
Modulus | % | Computes the remainder of the integer division | rem= a%b |
Syntax
The general form of the C arithmetic expression is as follows:
<variable_to_store> = operand1 arthOperator operand2 ;
For example, to add two numbers and store the value of the arithmetic
expression to a variable sum:
sum = 10 + 5 ;
To add two variables:
sum = number1 + number2 ;
where sum, number1, and number2 are variables.
Sample C Program
/************************************ * C Arithmetic Operators * Filename: arithmetic.c * C Arithmetic Operators Demo * program. * C Tutorials - www.TestingDocs.com **************************************/ #include<stdio.h> int main() { //Declare two variables int a=22,b=5; int sum,difference,product,rem; double div; sum = a + b; // addition difference = a - b; // subtraction product = a*b; // product div = a/b; // divide rem = a%b; // get remainder // Print Statements printf ("Sum = %d \n", sum); printf ("Difference = %d \n", difference); printf ("Product = %d \n", product); printf ("Division = %lf \n", div); printf ("Modulus = %d \n", rem); printf ("\nC Tutorials - www.TestingDocs.com\n"); return 0; } // end main
Program Output
Sum = 27
Difference = 17
Product = 110
Division = 4.000000
Modulus = 2