[go: up one dir, main page]

0% found this document useful (0 votes)
34 views10 pages

Lab Report 4 & 5

This lab report summarizes 6 programming tasks completed by Malik Musa Sarwar and Aiman Zaib for their Programming Fundamentals course. The tasks involved writing C++ programs to print natural numbers from 1 to n using a for loop, calculate factorials using a for loop, print the first 10 Fibonacci numbers using a for loop, print a multiplication table for a user-input number using a for loop, print a character pyramid pattern using nested for loops, and print a right-justified number pyramid using for loops. Completing these exercises provided experience with C++ concepts like input handling, arithmetic, conditions, and loops.

Uploaded by

nomanburushunda1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

Lab Report 4 & 5

This lab report summarizes 6 programming tasks completed by Malik Musa Sarwar and Aiman Zaib for their Programming Fundamentals course. The tasks involved writing C++ programs to print natural numbers from 1 to n using a for loop, calculate factorials using a for loop, print the first 10 Fibonacci numbers using a for loop, print a multiplication table for a user-input number using a for loop, print a character pyramid pattern using nested for loops, and print a right-justified number pyramid using for loops. Completing these exercises provided experience with C++ concepts like input handling, arithmetic, conditions, and loops.

Uploaded by

nomanburushunda1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab Report No 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

Department of Computer Science


HITEC University, Taxila
Lab Task No 01:

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;
}

The results (Screenshot)


Lab Task No 02:

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;
}

The results (Screenshot)

Lab Task No 03:

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;
}

The results (Screenshot)

Lab Task No 04:

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;
}

The results (Screenshot)

Lab Task No 05:


Question:
Write a program which prints half pyramid of character using for loop.

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>

using namespace std;

int main()
{
int rows;
cout << "Enter the number of rows: ";
cin >> rows;

for (int i = 1; i <= rows; i++)


{
char ch = 'A';
for (int j = 1; j <= i; j++)
{
cout << ch;
ch++;
}
cout<<endl;
}
return 0;
}

The results (Screenshot)

Lab Task No 06:

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

pyramid from the right side by incrementing the number.


The code
#include <iostream>
using namespace std;

int main()
{
int i, j, space, rows, k = 1;
cout << "Enter the number of rows: ";
cin >> rows;

for (i = 1; i <= rows; i++)


{
for (space = i; space < rows; space++)
{
cout << "\t";
}
for (j = 1; j <= i; j++)
{
cout << k << "\t";
k++;
}
cout << "\n";
}

return 0;
}

The results (Screenshot)


Conclusion Of Whole Lab Exercises :
In summary, the programs explored in this report have provided practical hands-on experience with

fundamental C++ concepts. These exercises encompassed input handling, arithmetic operations,

conditional statements and loops will contribute to a better grasp of C++ programming fundamentals.

You might also like