Lesson 02
Lesson 02
int main()
int number1;
int number2;
int result;
cout<<result;
return 0;
}
In the above program the variable number1 and number2 are called operands and they are connected via
different operators in expressions given on line numbers 16, 17, 18, 19 20. Response of each operation is
stored in the variable result.
Keep in mind that modulus (%) operator is only defined for the data type integers
It is important to emphasis that result of the division is not as we expect in general. This is because the data
type of number1 and number2 is integer, an integer divided by an integer will give an integer response,
while truncating the decimal part of the value. This makes the order of precedence of arithmetic operators
very significant. Consider the following example.
Page 1 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
y = 5 / 2 * 5 + 3 * 5 + 7;
cout<<y;
y = 5 * 5 / 2 + 3 * 5 + 7;
cout<<y;
les_02_code_02.cpp
1. #include<iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7.
8. int number1 = 74, number2 = 82, number3 = 88;
9. double average;
10. average = number1 + number2 + number3 / 3;
11. cout<<average;
12. return 0;
13. }
Page 2 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
les_02_code_03.cpp
1. #include<iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7.
8. int number1 = 74, number2 = 82, number3 = 88;
9. double average;
10. average = (number1 + number2 + number3) / 3;
11. cout<<average;
12. return 0;
13. }
If the value of constant is tried to be changed during the program the compiler will throw an error
Page 3 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Page 4 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Output
Task: Write a program that takes the angle input from the user in degrees and find its
cos, sin and tan.
Page 5 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Output
Page 6 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Output
Page 7 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Output
les_02_code_09.cpp
1. #include<iostream>
2. #include<cmath>
3.
4. using namespace std;
5.
6. int main ()
7. {
8. cout<<"fmod(5.3, 2.0) = "<<fmod(5.3,2)<<endl;
9. cout<<"fmod(18.5, 4.2) = "<<fmod (18.5,4.2)<<endl;
10. return 0;
11. }
Output
les_02_code_10.cpp
1. #include<iostream>
2. #include<cmath>
3.
4. using namespace std;
5.
6. int main ()
7. {
8. float num1 = -9.5;
9. cout<<fabs(num1);
10.
11. return 0;
12. }
Page 8 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Write a program that takes a floating point input from the user, finds its square, cube,
square root, floor, truncate, ceiling and round off, natural log, base 10 log.
Type Casting
les_02_code_11.cpp
1. #include<iostream>
2. #include<cmath>
3.
4. using namespace std;
5.
6. int main ()
7. {
8. float num1 = -9.5;
9. int num2 = 101;
10. cout<<(int)num1;
11. cout<<endl<<(float)num2/10;
12.
13. return 0;
14. }
Output
Page 9 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
Output
Page 10 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
les_02_code_13.cpp
1. #include<iostream>
2. #include<cmath>
3. #include<iomanip>
4.
5. using namespace std;
6.
7. int main()
8. {
9.
10. cout<<0.1234567809<<endl;
11. cout<<setprecision(10)<<0.1234567809<<endl;
12. cout<<setprecision(12)<<0.123412341234<<endl;
13. cout<<setprecision(8)<<0.12341234<<endl;
14. return 0;
15. }
Output
les_02_code_14.cpp
1. #include<iostream>
2. #include<iomanip>
3. #include<cmath>
5. int main()
6. {
7. double num1 = 2.3,num2 = 3.8,num3 = 5.5,num4 = -2.3,num5 = -
3.8,num6 = -5.5;
8. cout<<setw(8)<<"value"<<setw(8)<<"round"<<setw(8)<<"floor"<<setw(
8)<<"ceil"<<setw(8)<<"trunc\n";
9. cout<<setw(8)<<"-----"<<setw(8)<<"-----"<<setw(8)<<"-----
"<<setw(8)<<"-----"<<setw(8)<<"-----\n";
10. cout<<setw(8)<<num1<<setw(8)<<round(num1)<<setw(8)<<floor(num1)<<
setw(8)<<ceil(num1)<<setw(8)<<trunc(num1)<<"\n";
11. cout<<setw(8)<<num2<<setw(8)<<round(num2)<<setw(8)<<floor(num2)<<
setw(8)<<ceil(num2)<<setw(8)<<trunc(num2)<<"\n";
12. cout<<setw(8)<<num3<<setw(8)<<round(num3)<<setw(8)<<floor(num3)<<
setw(8)<<ceil(num3)<<setw(8)<<trunc(num3)<<"\n";
Page 11 of 12
EE-163(C&P) lesson_02 EED MHUH, FR
13. cout<<setw(8)<<num4<<setw(8)<<round(num4)<<setw(8)<<floor(num4)<<
setw(8)<<ceil(num4)<<setw(8)<<trunc(num4)<<"\n";
14. cout<<setw(8)<<num5<<setw(8)<<round(num5)<<setw(8)<<floor(num5)<<
setw(8)<<ceil(num5)<<setw(8)<<trunc(num5)<<"\n";
15. cout<<setw(8)<<num6<<setw(8)<<round(num6)<<setw(8)<<floor(num6)<<
setw(8)<<ceil(num6)<<setw(8)<<trunc(num6)<<"\n";
16. return 0;
17. }
Output
Exercise
Write a note on the importance of operator precedence, also emphasis why your
portable calculator don’t cause that problem.
Try to use cmath library functions with integer data types and find out the possible
problems.
Page 12 of 12