PROGRAMMING FUNDAMENTALS (CS-308)
NAME: Faizan Sharif
AG: 2025-ag-11396
LAB REPORT # 5
CLASS: B.S C. S
SECTION:2nd E
DEPARTMENT OF COMPUTER SCIENCE, PARS
UNIVERSITY OF AGRICULTURE, FAISALABAD
LAB REPORT # 5
Activity 1: For Loop
Q#1: Calculate the sum of numbers from 1 to 100 and display it on screen by
using for loop.
• Practical Solution#1:
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for(int i = 1; i <= 100; i++) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
• Output#1:
Q#2: Calculate sum of even and off numbers separately from a user given
range by using for loop. Like if user gives lower_limit of 50 and
higher_limit of 80. Calculate sum of all odds and evens in between 50 and
80 and display on screen.
• Practical Solution#2:
#include <iostream>
using namespace std;
int main() {
int low, high, evenSum = 0, oddSum = 0;
cout << "Enter lower limit: ";
cin >> low;
cout << "Enter higher limit: ";
cin >> high;
for(int i = low; i <= high; i++) {
if(i % 2 == 0)
evenSum += i;
else
oddSum += i;
cout << "Even Sum = " << evenSum << endl;
cout << "Odd Sum = " << oddSum << endl;
return 0;
}
• Output#2:
Q#3: Input the marks of a class of ten students. The marks must be
between 0 to 100. Also check marks should not be less than 0 or greater
than 100. Calculate the grades of every student and display it by using for
loop. Also calculate the total of the grades and the class average.
• >90 = A
• <90 and >80 = B
• <80 and >70 = C
• < 70 and > 60 = D
• <60 = F
• Practical Solution#3:
#include <iostream>
using namespace std;
int main() {
int marks, total = 0;
for(int i = 1; i <= 10; i++) {
cout << "Enter marks of student " << i << ": ";
cin >> marks;
if(marks < 0 || marks > 100) {
cout << "Invalid marks! Try again.\n";
i--;
continue;
total += marks;
if(marks > 90)
cout << "Grade: A\n";
else if(marks > 80)
cout << "Grade: B\n";
else if(marks > 70)
cout << "Grade: C\n";
else if(marks > 60)
cout << "Grade: D\n";
else
cout << "Grade: F\n";
cout << "Total = " << total << endl;
cout << "Average = " << total / 10.0;
return 0;
• Output#3:
Q#4: Input a number from user and display a pattern like these
on screen by using for loop. For example, user has entered 5, the
program should display following pattern on screen:
(a) * * * * *
****
***
**
*
(b) 1 2 3 4 5
1234
123
12
1
(c) 1
12
123
1234
12345
(d) ? ? ? ? ?
?????
?????
?????
?????
• Practical Solution# (4A):
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number: ";
cin >> n;
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
• Output# (4A):
• Practical Solution# (4B):
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number: ";
cin >> n;
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
• Output# (4B):
• Practical Solution# (4C):
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number: ";
cin >> n;
for(int i = 1; i <= n; i++) {
// spaces
for(int s = 1; s <= (n - i); s++) {
cout << " ";
}
// numbers
for(int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
• Output# (4C):
• Practical Solution# (4D):
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number: ";
cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
• Output# (4D):
Q#5: Calculate sum of even and off numbers by using do-while loop
separately from a user given range by using for loop. Like if user gives
lower_limit of 50 and higher_limit of 80. Calculate sum of all odds and even
in between 50 and 80 and display on screen.
• Practical Solution#5:
#include <iostream>
using namespace std;
int main() {
int low, high;
int evenSum = 0, oddSum = 0;
cout << "Enter lower limit: ";
cin >> low;
cout << "Enter upper limit: ";
cin >> high;
int i = low;
do {
if(i % 2 == 0)
evenSum += i;
else
oddSum += i;
i++;
} while(i <= high);
cout << "Even Sum = " << evenSum << endl;
cout << "Odd Sum = " << oddSum << endl;
return 0;
}
• Output#5:
Q#6: Print the table of user given number from 1 up to user given limit by
sing for loop. Like if user enters 3, and limit is 4, program should print
table of 3 as follows:
• 3 * 1 = 03
• 3 * 2 = 06
• 3 * 3 = 09
• 3 * 4 = 12
• Practical Solution#6:
#include <iostream>
using namespace std;
int main() {
int num, limit;
cout << "Enter number: ";
cin >> num;
cout << "Enter limit: ";
cin >> limit;
for(int i = 1; i <= limit; i++) {
cout << num << " * " << i << " = " << num * i << endl;
}
return 0;
}
• Output#6:
Q#7: Write a program to find the factorial of a number using a for loop.
• Practical Solution#7:
#include <iostream>
using namespace std;
int main() {
int n;
long long fact = 1;
cout << "Enter number: ";
cin >> n;
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
cout << "Factorial = " << fact;
return 0;
}
• Output#7:
Q#8: Write a program that takes an integer and prints its reverse using a
for loop.
• Practical Solution#8:
#include <iostream>
using namespace std;
int main() {
int num, rev = 0;
cout << "Enter number: ";
cin >> num;
while(num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}
cout << "Reverse = " << rev;
return 0;
}
• Output#8:
Q#9: Write a program to check whether a number is a palindrome (same
forward and backward) using a for loop.
• Practical Solution#9:
#include <iostream>
using namespace std;
int main() {
int num, original, rev = 0;
cout << "Enter number: ";
cin >> num;
original = num;
while(num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}
if(original == rev)
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}
• Output#9:
Q#10: Write a program to calculate x^y (x raised to the power y) using a
for loop.
• Practical Solution#10:
#include <iostream>
using namespace std;
int main() {
int x, y;
long long result = 1;
cout << "Enter base: ";
cin >> x;
cout << "Enter power: ";
cin >> y;
for(int i = 1; i <= y; i++) {
result *= x;
}
cout << "Answer = " << result;
return 0;
}
• Output#10:
Q#11: Print a diamond shape on screen by using for loop.
*
***
*****
*******
*****
***
*
• Practical Solution#11:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number: ";
cin >> n;
// upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i; j++)
cout << " ";
for(int k = 1; k <= (2*i - 1); k++)
cout << "*";
cout << endl;
}
// lower part
for(int i = n - 1; i >= 1; i--) {
for(int j = 1; j <= n - i; j++)
cout << " ";
for(int k = 1; k <= (2*i - 1); k++)
cout << "*";
cout << endl;
}
return 0;
}
• Output#11:
{End Of Lab}