C++ Arithmetic Operators

 C++ Arithmetic Operators:


C++ uses operators to do arithmetic. It provides operators for five basic arithmetic calculations: 
addition, subtraction, multiplication, division, and taking the modulus. 

Each of these operators uses two values (called operands) to calculate a final answer. 
Together, the operator and its operands constitute an expression. For example, consider the 
following statement:

int a = 4 + 2;
The values 4 and 2 are operands, the + symbol is the addition operator, and 4 + 2 is an 
expression whose value is 6.

Here are C++'s five basic arithmetic operators:

The + operator adds its operands. For example, 4 + 20 evaluates to 24.

The - operator subtracts the second operand from the first. For example, 12 - 3 evaluates to 9.

The * operator multiplies its operands. For example, 28 * 4 evaluates to 112.

The / operator divides its first operand by the second. For example, 1000 / 5 evaluates to 200. 
If both operands are integers, the result is the integer portion of the quotient. For example, 17 / 3 is 5,

with the fractional part discarded.

The % operator finds the modulus of its first operand with respect to the second. 
That is, it produces the remainder of dividing the first by the second. For example, 19 % 6 is 1 
because 6 goes 
into 19 three times, with a remainder of 1. Both operands must be integer types; using the 
% operator with floating-point 
values causes a compile-time error. If one of the operands is negative, the sign of the 
result depends
 on the implementation.

example:

#include <iostream>
#include<conio.h>
 
main() 
{
   int a = 21;
   int b = 10;
   int c ;
 
   c = a + b;
   cout << "Line 1 - Value of c is :" << c << endl ;
   
   c = a - b;
   cout << "Line 2 - Value of c is  :" << c << endl 
   ;
   c = a * b;
   cout << "Line 3 - Value of c is :" << c << endl ;
   
   c = a / b;
   cout << "Line 4 - Value of c is  :" << c << endl ;
   
   c = a % b;
   cout << "Line 5 - Value of c is  :" << c << endl ;
   
   c = a++; 
   cout << "Line 6 - Value of c is :" << c << endl ;
   
   c = a--; 
   cout << "Line 7 - Value of c is  :" << c << endl ;
   
   return 0;
}


Post a Comment

Previous Post Next Post