Tasks OOP Lab 02

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Programming language:

A set of rules, symbols, and special words.


Comments
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.

cout << "7 + 8 = " << 7 + 8 << endl; //prints: 7 + 8 = 15

/*
You can include comments that can
occupy several lines.
*/
Special Symbols
+ - * /
. ; ? ,
<= != == >=
Reserved Words (Keywords)
int, float, double, char, const, void, return
Simple Data Types

Lab Exercise Lab Exercise


#include <iostream> Write a program that gives the following output:
using namespace std;
int main()
{
int num;
num = 6; Sample Run:
cout << "My first C++ program." << endl;
cout << "The sum of 2 and 3 = " << 5 << endl; 5.0 + 3.5 = 8.5
cout << "7 + 8 = " << 7 + 8 << endl; 3.0 + 9.4 = 12.4
cout << "Num = " << num << endl; 16.3 - 5.2 = 11.1
return 0; 4.2 * 2.5 = 10.5
} 5.0 / 2.0 = 2.5
Sample Run: (When you compile and execute this program, 34.5 / 6.0 = 5.75
the following four lines are
34.5 / 6.5 = 5.30769
displayed on the screen.)
My first C++ program.
The sum of 2 and 3 = 5
7 + 8 = 15
Num = 6
Lab Task 1: Write a program that gives the following output:

Triangle Circle
Area = ½ × b × h Area = π × r2
b = base Circumference = 2 × π × r
h = vertical height r = radius

Square Rectangle
Area = a2 Area = w × h
a = length of side w = width
h = height

Parallelogram
Area = b × h
b = base
h = vertical height

Flow of execution of a while loop.


Lab Task 2: Write a Program in which user input different numbers on
the output window, if user put -999 then program stops and give sum,
average of all previous numbers. Also give the largest number from all
of the previous number.

Lab Task 3: Write a program in which user put any value on output
window, and program give the counting up to that number using
do…while loop.

Flow of execution of a for loop:


Lab Exercise Lab Exercise
Suppose that i is an int variable.
1. Consider the following for loop: In this program, a for loop reads five numbers and finds
for (i = 10; i <= 9; i++) their sum and average.
cout << i << " ";
cout << endl; Consider the following program code, in which i,
In this for loop, the initial statement sets i to 10. Because newNum, sum, and average are
initially the loop int variables.
condition (i <= 9) is false, nothing happens. sum = 0;
2. Consider the following for loop: for (i = 1; i <= 5; i++)
for (i = 9; i >= 10; i--) {
cout << i << " "; cin >> newNum;
cout << endl; sum = sum + newNum;
In this for loop, the initial statement sets i to 9. Because }
initially the loop condition average = sum / 5;
(i >= 10) is false, nothing happens. cout << "The sum is " << sum << endl;
3. Consider the following for loop: cout << "The average is " << average << endl;
for (i = 10; i <= 10; i++) //Line 1
cout << i << " "; //Line 2
cout << endl; //Line 3
In this for loop, the initial statement sets i to 10. The loop
condition (i <= 10)
evaluates to true, so the output statement in Line 2
executes, which outputs 10.

Lab Task 5: Write a program that creates the following pattern using
Nested for:
*
**
***
****
*****
Lab Task 6: Write a program that creates the following pattern using
Nested for:

Please Print all Lab Tasks and staple the pages


here

You might also like