Programming Fundamentals
University of Management and Technology,
Lahore Campus
Lab Manual
Instructor: Waqar Ashiq
Lecturer, Software Engineering Department
Email: waqar.ashiq@umt.edu.pk
for Loop and Nested for loop
Coding Examples:
for Loop:
• Loop: a control structure that causes a statement or statements to repeat
• Useful for counter-controlled loop
• General Format:
for(initialization; test; update)
statement; // or block in { }
• No semicolon after the update expression or after the )
for Loop - Mechanics
for(initialization; test; update)
statement; // or block in { }
1) Perform initialization
2) Evaluate test expression
– If true, execute statement
– If false, terminate loop execution
3) Execute update, then re-evaluate test expression
Programming Fundamentals
Example:
Code:
int count;
for (count = 1; count <= 5; count++)
cout << "Hello" << endl;
Execution Process:
Flow Chart:
Programming Fundamentals
Example 1: C++ Program to Find Average of N Numbers using
for loop.
#include <iostream>
using namespace std;
int main()
{
int num;
float total = 0, y;
float average;
cout << "Enter the number of elements to calculate the average::\n";
cin >> num;
cout << "Enter " << num << " elements one by one\n\n";
for(int i = 1; i <= num; i++)
{
cout<<"Enter the number: "<<i<<" :";
cin >> y;
total =total+ y;
}
average = total/ num;
cout << "\nThe average of the entered input numbers is = " << average;
return 0;
}
Programming Fundamentals
Example 2: Printing and counting odd and even numbers using for Loop
#include<iostream>
using namespace std;
int main()
{
int i,n,even=0,odd=0;
cout<<"\nEnter the Ending value:";
cin>>n;
cout<<"\nEven numbers:";
for(i=0;i<=n;i++)
{
if(i%2==0)
{
cout<<"\n"<<i;
even++;
}
}
cout<<"\nOdd numbers:";
for(i=1;i<=n;i++)
{
if(i%2==1)
{
cout<<"\n"<<i;
odd++;
}
Programming Fundamentals
}
cout<<"\nTotal even numbers:"<<even;
cout<<"\nTotal odd numbers:"<<odd;
return 0;
}
Nested for loop
Example 1: C++ program to display 7 days a weeks.
// C++ program to display 7 days a weeks
#include <iostream>
using namespace std;
int main() {
int weeks = 1, days_in_week = 7;
for (int i = 1; i <= weeks; ++i) {
cout << "Week: " << i << endl;
for (int j = 1; j <= days_in_week; ++j) {
cout << " Day:" << j << endl;
}
}
return 0;
}
Programming Fundamentals
Example 2: Program to Print a Half-Pyramid Using *
*
**
***
****
*****
Solution:
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << "\n";
}
return 0;
Programming Fundamentals
Example 3: Program to Print a Half-Pyramid Using Numbers.
1
12
123
1234
12345
Solution:
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
cout << j << " ";
}
cout << "\n";
}
return 0;
}
Programming Fundamentals
Example 4: Program to Print a Inverted Half-Pyramid Using *.
*****
****
***
**
*
Solution:
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Programming Fundamentals
Example 5: Write a program in c++ for Inverted Half-Pyramid Using
Number.
12345
1234
123
12
1
Solution:
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
Programming Fundamentals
return 0;
}