arthimectic of cpp
arthimectic of cpp
CPEN-1214
Lab 05: C++ Arithmetic
Operation and IF statements
LAB-02
1: Purpose:
Objective of this lab is to make students understand the arithmetic operations in C++ and simple
branching with IF Else.
2: Activity Outcomes:
The student will be to understand and write arithmetic operations in C++.
The student will be able to understand and write IF ELSE conditions.
3: Arithmetic in C++:
When we write code in C++ to do calculations, it is important to remember that results of integer and
integer calculations may be different than real and real calculations. We also need to know how mixed
number calculations will be carried out. C++ will allow you to assign a number with decimal to an integer,
however, the fractional part will be discarded. Program below explores various arithmetic calculations.
PROGRAM:
INTEGER OPERATIONS
6+3=9
10 / 3 = 3
10 % 3 = 1
10 / 3 = 3
10.0 / 3 = 3.33333
Explanation of Result:
If l=6.5 and m =3.5 then l+m should be 10, why is it 9? We assigned these numbers to integer variables,
which discards the fractional part leaving 6 and 3, which give a total of 9. How about 10/9 yielding 3?
This is called integer division. The next problem 10 % 3 gives a result of 1, which is the remainder of the
integer division (also known as the modulus). In the next problem even though we assigned the result of
10/3 to a real variable (a), the variable only received the result of an integer division. The result of
10.0/3.0 is 3.333333; here both numbers are real numbers (float). However the last problem 10.0/3 also
gives 3.33333, why? In a mixed operation like this the integer is converted to float first, then the operation
is carried out.
You can use these conditions to perform different actions for different decisions.
THE IF STATEMENT:
USE THE IF STATEMENT TO SPECIFY A BLOCK OF C++ CODE TO BE EXECUTED IF A CONDITION IS TRUE.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example:
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
Output: x is greater than y
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example:
USE THE ELSE IF STATEMENT TO SPECIFY A NEW CONDITION IF THE FIRST CONDITION IS FALSE.
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false
}
Example:
1. #include <iostream>
2.
3. using namespace std;
4.
5. main() {
6. int a = 21;
7. int b = 10;
8. int c;
9.
10. if (a == b) {
11. cout << "Line 1 - a is equal to b" << endl;
12. } else {
13. cout << "Line 1 - a is not equal to b" << endl;
14. }
15.
16. if (a < b) {
17. cout << "Line 2 - a is less than b" << endl;
Output:
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or euqal to b
Line 5 - b is either greater than or equal to b
Program2: Input Marks of three subjects from user, print average and also print ‘pass’ if average is
greater than or equal to 60 else print ‘fail’.
1. #include <iostream>
2.
3. using namespace std;
4.
5. int main() {
6.
7. int math, phy, comp;
8.
9. cout << "Enter marks of maths";
10. cin >> math;
11.
12. cout << "Enter marks of physics";
13. cin >> phy;
14.
15. cout << "Enter marks of computer";
16. cin >> comp;
17.
18. float average;
19.
20. average = (math + phy + comp) / 3;
21. cout << "Average of your marks is=" << average << endl;
22.
23. if (average >= 60) {
6: Practice Exercise:
Q-1: Write a program to display the name of the month, given its number. For example, if you enter 4 for
the month, it should display April. Write it using if/else.