Arithmetic Operators Arithmetic Operators C Uses four normal

  • Slides: 10
Download presentation
Arithmetic Operators

Arithmetic Operators

Arithmetic Operators • C++ Uses four normal Arithmetic operators. These Operators work on all

Arithmetic Operators • C++ Uses four normal Arithmetic operators. These Operators work on all the data types such as integer and floating-point. These arithmetic operators are 1. + for addition 2. - for subtraction 3. * for multiplication 4. / for division • Example int x=4, y =3, z=5, result; result = x + y - z; cout<< “result=“<<result; result = (x * y) + (z/x); cout<<“result=“ << result;

Remainder Operator • There is a fifth arithmetic operator that works only with integer

Remainder Operator • There is a fifth arithmetic operator that works only with integer variables such as char, short, int, and long. • This remainder operator is presented by % and is also called modulus operator. • This operator finds the remainder when one number is divided by another. • Example void main() { cout<< 6 % 8; // 6 cout<< 8 % 8; //0 cout<< 10 % 8; //2 }

Rules of Operator Precedence 1. Operators in expressions contained within pairs of parentheses are

Rules of Operator Precedence 1. Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be at the "highest level of precedence. " ((a + b) + c) 2. Multiplication, division and modulus operations are applied next. If an expression contains several multiplication, division and modulus operations, operators are applied from left to right. Multiplication, division and modulus are said to be on the same level of precedence. 3. Addition and subtraction operations are applied last. If an expression contains several addition and subtraction operations, operators are applied from left to right. Addition and subtraction also have the same level of precedence. a * b + ( c – d) / f

Arithmetic Assignment Operators • The following kind of statement is common in most languages

Arithmetic Assignment Operators • The following kind of statement is common in most languages total = total + item; // adds ‘item’ in ‘total’ • As total appears twice. So, C++ offers a condensed approach i. e. Arithmetic Assignment operators. • Arithmetic assignment operator combines an arithmetic operator and assignment operator and eliminates the repeated operand. • So the above statement can be written as total += item; // adds ‘item’ in ‘total’ • You can write arithmetic assignment operators with other arithmetic operators as well. • Example += , -=, *= , /= and %=

Example of Arithmetic assignment operator void main () { int result = 27; result

Example of Arithmetic assignment operator void main () { int result = 27; result += 10; cout<< result; result -= 10; cout<< result; result *= 10; cout<< result; result /= 10; cout<< result; result %= 10; cout<< result; } // same as result = result + 10; // same as result = result - 10; // same as result = result * 10; // same as result = result / 10; // same as result = result % 10;

Increment Operators • You often need to add 1 to the value of an

Increment Operators • You often need to add 1 to the value of an existing variable. Normal way to do this is count = count + 1; • Or using an arithmetic assignment operator count += 1; • New approach is ++count; The ++ operator increments its arguments.

Increment Operators • Prefix and Postfix – The increment operator can be used in

Increment Operators • Prefix and Postfix – The increment operator can be used in two ways • Prefix: meaning that the operator precedes the variable • Postfix: meaning that operator follows the variable. Example: 1. result = result * ++count; Note: In this scenario count is incremented first than multiplied by result. 2. result = result * count++

Increment Operator Example void main () { int count = 10; cout<< “count =“

Increment Operator Example void main () { int count = 10; cout<< “count =“ <<count; cout<< “count=”<< ++count; cout<< “count=”<<count++; cout<<“count=”<<count; } // count=10 //count = 11 //count = 12

Exercise 1. Asghar bought five items form shopping mall. Write a program that takes

Exercise 1. Asghar bought five items form shopping mall. Write a program that takes prices of all the five items as input through keyboard and than calculates the total bill including 5 % sales tax. 2. Ali basic salary is input through keyboard. His house allowance is 30% of basic salary, and medical allowance is 20 % of basic salary. Write a program to calculate his gross salary. 3. If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.