Professional Documents
Culture Documents
Lecture No.05
Lecture No.05
Allah
The Most Merciful and Compassionate the most
gracious and beneficent, Whose help and
guidance we always solicit at every step, at every
moment.
Programming Concepts
Shabir Ahmad
Arithmetic Operators
Precedence of Operators
Compound Assignment Statement
Compound Assignment Expression
Increment Operator(++)
Decrement Operator(--)
1. 4+3*3-6 = 7
2. (4+3)*3-6 = 15
3. 5+5%3 = 7
4. 5%2*3 = 3
5. (8*5%3)*5+10 = 15
6. 105/5*5%3+2 = 2
7. -(-10/2)*2 = 10
8. 128%3%2 = 0
Problem Statement:
Write a program to assign a value 6 to integer type variables x, y, a, b, c. Also
calculate the sum of the variables and print the result on the screen.
1. #include<iostream.h>
2. #include<conio.h>
3. main() OUT PUT
4. {
5. int x,y,a,b,c,s; The sum=30
6. x=y=a=b=c=6;
7. s=x+y+a+b+c;
8. cout<<“The sum=”<<s;
9. getch();
10. }
For Example:
To add 10 to the variable x that already has a value, the simple arithmetic
statement is written as:
x= x + y; can be written as x += 10;
Following are the examples of compound assignment expressions:
x += 9; // same as x = x + 9;
x -= 9; // same as x = x - 9;
x *= 9; // same as x = x * 9;
x /= 9; // same as x = x / 9;
Problem Statement:
Write a program to assign 3 values to three integer type variables a ,b & c. Add
variables as a & b and multiply their sum to variable c.
1. #include<iostream.h>
2. #include<conio.h>
3. Main()
4. { OUT PUT
5. int a,b,c;
The result=45
6. a=6;
7. b=9;
8. c=3;
9. c *= a+b;
10. cout<<“The result =”<<c;
11. getch();
12. }
Problem Statement:
Write a program to show the effect of increment operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. Int a, b,c,s; OUT PUT
6. a=1; Sum of prefixing =6
c=4
7. b=1;
8. c=3;
9. s =a+b+(++c);
10. cout<<“sum of prefixing=“<<s<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }
Problem Statement:
Write a program to show the effect of increment operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. Int a, b,c,p; OUT PUT
6. a=1; Sum of prefixing =5
c=4
7. b=1;
8. c=3;
9. p=a+b+(c++);
10. cout<<“sum of post fixing=”<<p<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }
Problem Statement:
Write a program to show the effect of decrement operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. Int a, b,c,s; OUT PUT
6. a=8; Sum of prefixing =12
c=2
7. b=2;
8. c=3;
9. s =a+b+(--c);
10. cout<<“sum of prefixing=“<<s<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }
Problem Statement:
Write a program to show the effect of decrement operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. int a, b,c,p; OUT PUT
6. a=8; Sum of prefixing =13
c=2
7. b=2;
8. c=3;
9. p=a+b+(c--);
10. cout<<“sum of post fixing=”<<p<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }
Thanks
Questions?