Lab 5
Computer Programming Lab
Lab Journal - 5
Name: _________________________________
Enrollment #: _________________________________
Date: ________________________________
Objective:
1) Understanding Looping Statements
2) Practicing for repetition structures
3) Practicing nested for repetition structures
Tools Required:
a) PC with Windows 7 Professional or onwards
b) Visual Studio 2013 onwards
A comparison among the three types of loops for you:
Same code written with different loops
For:
int j, square;
for (j = 0; j < 15; j++)
{
square = j * j;
cout << " square is : " <<square;
cout << endl;
}
While:
int j, square;
j = 0;
while (j < 15)
{
square = j * j;
cout << " square is : " << square;
cout << endl;
j++;
}
Computer Programming Lab
Page 1
Lab 5
Do-While:
int j, square;
j = 0;
do
{
square = j * j;
cout << " square is : " << square;
cout << endl;
j++;
} while (j < 15);
Attempt the following tasks:
Task 1 : Give output of the following pieces of codes, also write description in 1-2 lines that
what is happening in each code :
1. #include<iostream> Description:
#include<conio.h>
using namespace std;
void main()
{ Output:
int x = 2, y = 3, result = 1;
for(int i = 0; i < y; i++)
{
result *= x;
}
cout << result << endl;
getch();
}
2. //This program is written just to Description:
show you that for loop can be written
without initialization and increment
statement. You can see both these
statements are provided manually. Output:
You will not use for loop like this.
This is just to show you that this
thing also exists.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int num = 1;
for(; num <= 10;)
{
Computer Programming Lab
Page 2
Lab 5
cout << num << " ";
num++;
}
getch();
}
3. #include<iostream> Description:
#include<conio.h>
using namespace std;
void main()
{ Output:
int x=4, y=3;
for(int i=1;i<=y;i++)
{
for(int j=1;j<=x;j++)
cout<<"*";
cout<<endl;
}
getch();
}
4. #include<iostream> Description:
#include<conio.h>
using namespace std;
void main()
{
for(int i=1;i<=2;i++) Output:
{
for(int j=1;j<=2;j++)
{
for(int k=1;k<=3;k++)
cout<<"*";
cout<<endl;
}
cout<<endl;
}
getch();
}
5. Convert the following do-while loop to a Write Code through for loop here:
for loop, only initialize the loop
variable in the initialization part
int cube = 1;
int numb = 1;
do
{
cube = numb * numb * numb;
cout<< “ Number is: “ <<numb;
cout<< “ Cube is:
“<<cube<<endl;
numb++;
} while( numb < 30);
Computer Programming Lab
Page 3
Lab 5
Task 2: Write a C++ program using for loop, that takes a number from the user and displays its
table till 10 multiples. Output should be in the form 2 x 1 = 2
Task 3:
Write a program using for loop to extract even number from the range of integers given from the
keyboard and then print them on the screen. Your program output should be something like:
Input starting integer: 6
Input ending integer: 12
Even numbers are: 6 8 10 12
Task 4: Write a C++ program using nested for loop to print the following pyramid.
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Task 5: Write a C++ program using nested for loop to print the following pyramid.
*
**
***
****
*****
******
Task 6: Write a C++ program using nested for loop to print the following pyramid.
54321
54321
54321
54321
54321
Computer Programming Lab
Page 4
Lab 5
Task 7: Write a C++ program using nested for loop to print the following pyramid.
*
***
*****
*******
*********
****************************************************
Computer Programming Lab
Page 5