Department of Electrical Engineering
EE:101 Computing Fundamentals and Programming
Course Instructor: Mr. Syed Basit Ali Dated: 10th Nov, 2019
Lab Engineer: Usama Latif, Nazish Khalid Semester: 1st
Session: Fall2020 Batch: BSEE2020
Lab 8. Implementation of For Loop in C++ Using Visual Studio
Lab Report Viva Total
Name Roll No
Marks/10 Marks/5 Marks/15
Ahmad Raza BSEE16041
Checked on: _______________________________
Signature: __________________________________
a. Objective
The goal of this handout is to observe how a program works, how an output is displayed of
your work and also to get a know-how of how programming needs to be done step by step.
b. Equipment and Component
Component Description Value Quantity
Computer Available in lab 1
c. Conduct of Lab
1. Students are required to perform this experiment individually.
2. In case the lab experiment is not understood, the students are advised to seek help from
the course instructor, lab engineers, assigned teaching assistants (TA) and lab
attendants.
d. Theory and Background
In computer programming, loop repeats a certain block of code until some end condition is
met.
The syntax of a for loop is:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example:
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
e. Online Submission Instructions
● You have to submit .cpp files having name according to format yourRollNo.cpp in
small letters. For example bsee20001.cpp
● All the .cpp files should be in a folder having name according to format
yourRollNo.cpp in small letters. For example bsee20001
● You should print your roll number and name in the first line. Then you should leave a
line before displaying other output
f. Lab Tasks
Q.1: Output the square of number from 1-10, using for loop. [2 Marks]
#include <iostream>
using namespace std;
int main()
cout<<"\n The square of first ten integers are : "<<endl;
for(int num=1;num<=10;num++)
int square=num*num;
cout<<"\t square of number "<<num<<" = "<<num<<"*"<<num<<
" = "<<square<<endl;
return 0;
}
Q.2 Output 1 to n Natural Numbers and also output their sum using for loop. (n will define
by user).
[2 Marks]
#include <iostream>
using namespace std;
int main()
int n;
int sum = 0;
cout << "Enter the required number of n: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum = sum+i;
}
cout << "Sum = " << sum;
return 0;
Q.3: Write a program to add all number entered by user until user enters 0 using for loop
[2 Mark]
#include <iostream>
using namespace std;
int main()
int n;
int sum = 0;
cout << "Enter the required number of n: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum = sum+i;
}
while(n!=0);
cout << "Sum = " << sum;
return 0;
Q.4: Write a program to calculate Factorial of any number given by user. [2 Marks]
#include <iostream>
using namespace std;
int main() {
int number;
int factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>number;
for (int a=1;a<=number;a++) {
factorial=factorial*a;
cout<<"Factorial of number will be ="<<factorial<<endl;
return 0;
}
Q.5: Write a program to find prime number within range of numbers defined by user. [2 Marks]
#include <iostream>
using namespace std;
int main()
{
int a, b, i, j;
cout << "Enter the first number ";
cin >> a;
cout << "\enter the second number: ";
cin >> b;
cout << "\nPrime numbers between " << a << " and " << b
<< " will be: ";
if (a <= 2) {
a = 2;
if (b >= 2) {
cout << a << " ";
a++;
}
}
if (a % 2 == 0)
a++;
for (i = a; i <= b; i = i + 2) {
bool flag = 1;
for (j = 2; j * j <= i; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)
cout << i << " ";
}
return 0;
}
Conclusion:
I have learnt for loop in this lab.