0% found this document useful (0 votes)
2 views

arthimectic of cpp

The document outlines a lab exercise for students to learn C++ arithmetic operations and IF statements. It includes explanations of integer and real number calculations, conditional statements, and provides example programs demonstrating the use of relational operators and average calculations. The lab aims to enhance students' understanding of basic programming concepts in C++.

Uploaded by

Adeel Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

arthimectic of cpp

The document outlines a lab exercise for students to learn C++ arithmetic operations and IF statements. It includes explanations of integer and real number calculations, conditional statements, and provides example programs demonstrating the use of relational operators and average calculations. The lab aims to enhance students' understanding of basic programming concepts in C++.

Uploaded by

Adeel Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to ICT-Lab

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:

1. #include <iostream> //this is preprocessor directive


2.
3. using namespace std; //using directive
4. int main() //this is the main function
5. {
6. int i, j, k, l, m, n;
7. float a, b, c;
8. //integer operations
9. cout << "INTEGER OPERATIONS\n \n";
10. i = 6 + 3;
11. l = 6.5;
12. m = 3.5;
13. j = l + m;
14. k = 10 / 3;
15. n = 10 % 3;
16. cout << "6 + 3 = " << i << "\n";
17. cout << "l = 6.5, m = 3.5 --------->l + m = " << j << "\n";
18. cout << "10 / 3 = " << k << "\n";
19. cout << "10 % 3 = " << n << "\n";
20. //real and mixed operations
21. cout << "\nREAL AND MIXED OPERATIONS \n \n";
22. a = 10 / 3;
23. b = 10.0 / 3.0;
24. c = 10.0 / 3;
25. cout << "10 / 3 = " << a << "\n";
26. cout << "10.0 / 3.0 = " << b << "\n";
27. cout << "10.0 / 3 = " << c << "\n";
28. return 0;
29. }

Introduction to ICT – Lab [CPEN-1214] 1


Output:

INTEGER OPERATIONS

6+3=9

l = 6.5, m = 3.5 --------->l + m = 9

10 / 3 = 3

10 % 3 = 1

REAL AND MIXED OPERATIONS

10 / 3 = 3

10.0 / 3.0 = 3.33333

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.

4: C++ Conditions and If Statements:

C++ supports the usual logical conditions from mathematics:

 Less than: a < b


 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

Introduction to ICT – Lab [CPEN-1214] 2


 Use if to specify a block of code to be executed, if a specified condition is true
 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

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

THE ELSE STATEMENT:


USE THE ELSE STATEMENT TO SPECIFY A BLOCK OF CODE TO BE EXECUTED IF THE CONDITION IS
FALSE.

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:

int time = 20;


if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
Output: Good Evening

Introduction to ICT – Lab [CPEN-1214] 3


THE ELSE IF STATEMENT:

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:

int time = 22;


if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
Output: Good evening.
5: Examples:

Program1: Understanding different Relational Operators

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;

Introduction to ICT – Lab [CPEN-1214] 4


18.
19. }
20. else {
21. cout << "Line 2 - a is not less than b" << endl;
22. }
23.
24. if (a > b) {
25. cout << "Line 3 - a is greater than b" << endl;
26. } else {
27. cout << "Line 3 - a is not greater than b" << endl;
28. }
29.
30. /* Let's change the values of a and b */
31. a = 5;
32. b = 20;
33. if (a <= b) {
34. cout << "Line 4 - a is either less than \ or equal to b" << endl;
35. }
36.
37. if (b >= a) {
38. cout << "Line 5 - b is either greater than \ or equal to b" << endl;
39. }
40.
41. return 0;
42. }

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) {

Introduction to ICT – Lab [CPEN-1214] 5


24. cout << "Pass";
25. } else {
26. cout << "Fail";
27. }
28.
29. return 0;
30. }

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.

Introduction to ICT – Lab [CPEN-1214] 6


Introduction to ICT – Lab [CPEN-1214] 7

You might also like