Arithmetic Operators

In C language we have the usual arithmetic operators for addition, subtraction, multiplication and division. C also provides a special arithmetic operator which is called modulus. All these operators are binary operators which means they operate on two operands. So we need two values for addition, subtraction, multiplication, division and modulus.

Arithmetic OperationArithmetic OperatorAlgebraic ExpressionC Expression
Addition+x + yx + y
Subtraction-x - yx - y
Multiplication*xyx * y
Division/x ÷ y, x / yx / y
Modulus%x mod yx % y

Addition, subtraction and multiplication are same as we use in algebra.
There is one thing to note in division that when we use integer division (i.e. both operands are integers) yields an integer result. This means that if, for example, you are dividing 5 by 2 (5 / 2) it will give integer result as 2 instead of actual result 2.5. Thus in integer division the result is truncated to the whole number, the fractional part (after decimal) is ignored. If we want to get the correct result, then we should use float data type.

The modulus operator returns the remainder after division. This operator can only be used with integer operands. The expression x % y returns the remainder after x is divided by y. For example, the result of 5 % 2 will be 1, 23 % 5 will be 3 and 107 % 10 will be 7.

Sample Program 2
This is a sample program that uses the arithmetic operators. It displays the result of arithmetic operations on the screen. To make the result more visible we have used end line character (endl) with cout. So the statement:

cout << endl;

Simply ends the current line on the screen and the next cout statement displayed at the next line on the screen.

Code of The Program(Operation with integers)

Output of the program

Code of The Program(Operation with decimal numbers)

Output of the program

see everything clearly
// A Sample Program that uses Arithmetic Operators.

#include

main ()
{
//declaration of variables
int a;
int b;
int c;

float x;
float y;
float z;

// assigning values to variables
a = 23;
b = 5;
x = 12.5;
y = 2.25;

// processing and display with integer values
cout << "Operations with integers" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; //addition c = a + b; cout << "a + b = " << c << endl; //subtraction c = a - b; cout << "a - b = " << c << endl; //multiplication c = a * b; cout << "a * b = " << c << endl; // division c = a / b; cout << "a / b = " << c << endl; //modulus c = a % b; cout << "a % b = " << c << endl; // processing and display with decimal values cout << "Operations with decimal numbers" << endl; cout << "x = " << x << endl; cout << "y = " << y << endl; //addition z = x + y; cout << "x + y = " << z << endl; //subtraction z = x - y; cout << "x - y = " << z << endl; //multiplication z = x * y; cout << "x * y = " << z << endl; //division z = x / y; cout << "x / y = " << z << endl; } Output of the program
Operations with integers

a = 23
b = 5
a + b = 28
a - b = 18
a * b = 115
a / b = 4
a % b = 3

Operations with decimal numbers

x = 12.5
y = 2.25
x + y = 14.75
x - y = 10.25
x * y = 28.125
x / y = 5.55556

For previous lesson click here: Data Types
For next lesson click here: Precedence of Operators


the easiest way to learn programming
introduction to programming
Arithmetic Operators

0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger