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

CP Lab 5 Ex 1-5

This document contains 5 programming exercises assigned in a computer science lab. The exercises involve writing C++ programs to: 1) print Floyd's triangle, 2) print a diamond pattern, 3) display Pascal's triangle, 4) display a pattern with numbers starting from 1 and ending with 1 on each row, 5) display a pyramid pattern using alphabet letters. The document provides the full code for each programming exercise and is from a computer science lab journal for semester 1 of a bachelor's degree in computer science.

Uploaded by

Yahya Faisal
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)
86 views10 pages

CP Lab 5 Ex 1-5

This document contains 5 programming exercises assigned in a computer science lab. The exercises involve writing C++ programs to: 1) print Floyd's triangle, 2) print a diamond pattern, 3) display Pascal's triangle, 4) display a pattern with numbers starting from 1 and ending with 1 on each row, 5) display a pyramid pattern using alphabet letters. The document provides the full code for each programming exercise and is from a computer science lab journal for semester 1 of a bachelor's degree in computer science.

Uploaded by

Yahya Faisal
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

CSL-113: Computer Programming

Lab
Semester BS (CS) – 01
Yahya
02-134211-006
Lab 5

EXERCISE 1

1. Write a C++ program to print Floyd’s triangle as an output.

#include <iostream>
using namespace std;

Int main ()
{
int x, y, z, rows;
cout << "Enter number of rows : ";
cin >> rows;

for (x = 1; x <= rows; x++) {


for (y = 1; y <= rows - x; y++) {
cout << " ";
}
for (z = 1; z <= x; z++) {
if ((x + z) % 2 != 0) {
cout << "0";
}
else cout << "1"; {
}
cout << " ";
}
cout << endl;
}
cout << endl;
system ("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
EXERCISE 2

2. Write a C++ program to print a pattern like a diamond.

#include <iostream>
using namespace std;

int main ()
{
int i, j, r;
cout << "Enter number of rows : ";
cin >> r;
for (i = 0; i <= r; i++) {
for (j = 1; j <= r - i; j++) {
cout << " ";
}
for (j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
for (i = r - 1; i >= 1; i--) {
for (j = 1; j <= r - i; j++) {
cout << " ";
}
for (j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
system ("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
EXERCISE 3

3. Write a C program to display Pascal's triangle.

#include<iostream>
using namespace std;

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

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


{
int val = 1;
for (int j = 1; j < (rows - i); j++) {
cout << " ";
}
for (int k = 0; k <= i; k++)
{
cout << " " << val;
val = val * (i - k) / (k + 1);
}
cout << endl << endl;
}
system ("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
EXERCISE 4

4. Write a C++ program to display such a pattern for n number of rows using a
number that will start with the number 1 and a last number of each row will be 1.

#include <iostream>
using namespace std;

int main ()
{
int rows, i = 1;
cout << "Enter number of rows : ";
cin >> rows;
for (int x = 0; x < rows; x++) {
for (int a = 1; a <= rows - x; a++) {
cout << " ";
}
for (int y = 1; y <= x; y++) {
cout << y;
}
for (int y = x - 1; y >= 1; y--) {
cout << y;
}
cout << endl;
}
system ("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
EXERCISE 5

5. Write a C++ program to display pyramid pattern using alphabets.

#include <iostream>
using namespace std;

int main ()
{
int x, y, n, b;
char alph = 'A';
int c = 1;
cout << "Enter the number of the last letter : ";
cin >> n;
for (x = 1; x <= n; x++) {
for (b = 1; b <= n - x; b++)
cout << " ";
for (y = 0; y <= (c / 2); y++)
{
cout << alph++ << " ";
}
alph = alph - 2;
for (y = 0; y < (c / 2); y++)
{
cout << alph-- << " ";
}
c = c + 2;
alph = 'A';
cout << endl;
}
system ("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal

You might also like