Lab Report 4 & 5
Lab Report 4 & 5
Programming Fundamentals
Submitted By:
Malik Musa Sarwar & Aiman Zaib
23-CS-027 & 23-CS-175
Section A
Submitted to:
Ma’am Sibgha Batool
Question:
Write a program to print all natural numbers from 1 to n using for loop.
Solution:
Brief description (3-5 lines)
This C++ program takes a positive integer `n` as input, and using a for loop, it iterates from 1
to `n`, printing each natural number in sequence on the screen. It also includes input
validation to ensure `n` is a positive integer.
The code
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << i << " ";
};
return 0;
}
Question:
Compute factorial of a number using for loop.
Solution:
Brief description (3-5 lines)
This C++ program takes a number 'n' as input and calculates its factorial using a for loop,
storing the result in the 'factorial' variable. It then prints the factorial of 'n' on the screen
after input validation to handle negative numbers.
The code
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number to find its factorial: ";
cin >> n;
int factorial = 1;
for (int i = 1; i <= n; i++)
{
factorial = factorial * i;
}
cout << "Factorial of " << n << " is: " << factorial << endl;
return 0;
}
Question:
Print first 10 fibonacci number using for loop
Solution:
Brief description (3-5 lines)
In this C++ program, the first 10 Fibonacci numbers are printed using a for loop. It initializes
`n1` and `n2` as the first two Fibonacci numbers (0 and 1), and then the loop calculates and
prints the subsequent Fibonacci numbers by adding the previous two numbers, all without
the use of if-else statements.
The code
#include <iostream>
using namespace std;
int main()
{
int n = 10;
int n1 = 0, n2 = 1;
cout << "First " << n << " Fibonacci numbers: ";
for (int i = 0; i < n; i++)
{
cout<< n1 << " ";
int n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
return 0;
}
Question:
Print table of a number by taking the number from the user using for loop.
Solution:
Brief description (3-5 lines)
This C++ program takes a user-input number and then prints its multiplication table from 1 to
10 using a for loop. It iterates through the numbers 1 to 10, multiplying each number by the
user-input number and displaying the result
The code
#include <iostream>
using namespace std;
int main()
{
int n,i;
cout<<"Enter number for table: ";
cin>>n;
for(i=1;i<=10;i++)
{
cout<<n<<"x"<<i<<"="<<n*i<<endl;
}
return 0;
}
Solution:
Brief description (3-5 lines)
This C++ program takes the number of rows as input from the user and prints a pyramid
pattern. It starts with 'A' in the first row and increments the character for each subsequent
row. The program uses nested for loops to control row and character printing.
The code
#include <iostream>
int main()
{
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
Question:
Write a program which outputs half pyramids of number from right side of screen.
Solution:
Brief description (3-5 lines)
This program takes the number of rows from the user and prints the pattern of the half
int main()
{
int i, j, space, rows, k = 1;
cout << "Enter the number of rows: ";
cin >> rows;
return 0;
}
fundamental C++ concepts. These exercises encompassed input handling, arithmetic operations,
conditional statements and loops will contribute to a better grasp of C++ programming fundamentals.