0% found this document useful (0 votes)
80 views24 pages

Rizal Technological University: (D) Pascal's Triangle, Height 8

The document describes a laboratory activity on iteration/loop constructs in C++ programming. It contains 5 problems to implement different loop structures: [1] display patterns like inverted pyramid, right triangle, scalene triangle, Pascal's triangle, and tetrahedron using loops; [2] determine if a number is a palindrome or not using loops; [3] find the value of a polynomial for a given x value using Horner's method and loops. Pseudocode and flowcharts are provided to illustrate the algorithms and iteration processes for some of the problems.

Uploaded by

bong bong
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)
80 views24 pages

Rizal Technological University: (D) Pascal's Triangle, Height 8

The document describes a laboratory activity on iteration/loop constructs in C++ programming. It contains 5 problems to implement different loop structures: [1] display patterns like inverted pyramid, right triangle, scalene triangle, Pascal's triangle, and tetrahedron using loops; [2] determine if a number is a palindrome or not using loops; [3] find the value of a polynomial for a given x value using Horner's method and loops. Pseudocode and flowcharts are provided to illustrate the algorithms and iteration processes for some of the problems.

Uploaded by

bong bong
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/ 24

Rizal Technological University

Boni Avenue Mandaluyong City

Computer Programming 1 Lab

Laboratory Activity 3 – Iteration/Loop Constructs

Objectives:
1. Implement the C++ loop constructs (for loop . . . while loop . . . do while. . ) in
solving different problems.
2. Use iteration table to visualize the states of iterations

1. Write a menu-driven program to display each of the following patterns.


(a) inverted pyramid height = 7

***********
*********
*******
*****
***
*
(b) a right triangle height = 5
A
AB
ABC
ABCD
ABCDE
(c.) a scalene triangle
1
121
12321
1234321
123454321

(d)

Pascal’s Triangle, height = 8

(e)

Tetrahedron, height = 6
#include <iostream>

using namespace std;

int main()

char letter;

cout << "Number 1.\n\n";

cout << "Choose for the letter of choice\n";

cout << "\n==========================================================\n";

cout << "\n[A].Inverted Pyramid" << "\n" << "\n[B].Right Triangle" << "\n" << "\n[C].Scalene Triangle"
<< "\n" << "\n[D].Pascal Triangle"

<< "\n" << "\n[E].Tetrahedron\n";

cout << "\n==========================================================\n";

cout << "\nNote! capital letter only\n";

cout << "\nEnter the letter of your choice: ";

cin >> letter;

cout << endl;

if(letter == 'A'){

int height;

cout << "A.Inverted Pyramid\n\n";

cout << "Enter number of rows: ";

cin >> height;


cout << endl;

for(int i = height; i >= 1; --i)

for(int space = 0; space < height-i; ++space)

cout << " ";

for(int j = i; j <= 2*i-1; ++j)

cout << "* ";

for(int j = 0; j < i-1; ++j)

cout << "* ";

cout << endl;

} else if (letter == 'B'){

cout << "\nB.Right Triangle\n\n";

for(int i=1; i<=5;i++){

char alphabet = 'a';

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

cout << alphabet++;

} cout << endl;

} else if (letter == 'C'){

int h = 1, num;

cout << "C. Scalene Triangle\n\n";

cout << "Enter the number: ";


cin >> num;

cout << endl;

for(int i=1; i<= num; i++){

for(int k= 1; k<=i; k++){

cout << " ";

for(int j =1; j<=i;j++){

cout << h++;

} cout << endl;

} else if(letter == 'D'){

int rows, value;

cout << "Enter number of rows: ";

cin >> rows;

cout << endl;

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

value = 1;

for(int j= 0; j< rows-1-i;j++)

cout << " ";

for(int k=0;k<=i;k++){

cout << " " << value;

value = value*(i-k)/(k+1);

cout << endl << endl;

}
} else if (letter == 'E'){

int i,j,k,l,n;

cout<<"\n";

cout<<"TETRAHEDRON TRIANGLE\n"<<endl;

cout<<"Enter numbers of level: ";

cin>>n;

cout << endl;

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

for(k=n;k>=i;k--)

cout<<" ";

for(j=1;j<=(2*i-1);j++)

cout<<"#";

for(l=i*2;l>1;l--)

cout<<"!";

cout<<"\n";

return 0;

}
A. Program plan.

Choose for the letter of choice

A. Inverted Pyramid
B. Right Triangle
C. Scalene Triangle
D. Pascal Triangle
E. Tetrahedron

Enter the letter of your choice:

B. Algorithm.

Step 1: Initialize char letter;

Step 2: get letter .

Step 3: if letter equal to A print Inverted Pyramid.

Step 4: Else if letter equal to B print Right Triangle.

Step 5: Else if letter equal to c print Scalene Triangle.

Step 6: Else if letter equal to D print Pascal Triangle.

Step 7: Else if letter equal to B print Tetrahedron.

Step 8: End
Start

C. Flowchart.
char letter;

Get letter

if(letter == decrement
'A')

For i >= 1

space <
height-i

increment

Print << " ";

j <= 2*i-1

increment

Print << "* ";

increment j < i-1

Print << "* ";

End

End
D. Pseudocode.

Start Program.

Char letter;

Get letter;

If letter equal to A ; int height;

for(int i = height; i >= 1; --i)

for(int space = 0; space < height-i; ++space) print “ ”

for(int j = i; j <= 2*i-1; ++j) print “* ”

for(int j = 0; j < i-1; ++j) print “* ”

End
E. Iteration Table.

Iteration Variable height; i >= 1 Action


i >= 7 Not checked i is executed and
height is decreased by
6.
1st i >= 6 True i is executed and
height is decreased by
5.
2nd i >= 5 True i is executed and
height is decreased by
4.
3rd i >= 4 True i is executed and
height is decreased by
3.
4th i >= 3 True i is executed and
height is decreased by
2.
5th i >= 2 True i is executed and
height is decreased by
1.
6th i >= 1 True i is executed and
height is decreased by
0.
7th i >= 0 False The loop is terminated

3. A program that determines whether a number from the keyboard is a palindrome or not.

A. Program Plan.
Initialize int num, n, digit, rev =0
n=num

Enter the number: 909

The reverse number is: 909

The number is a palindrome

B. Algorithm.

Step 1: Initialize variable, int num, n, digit, rev = 0and n = num.

Step 2: Get num;

Step 3: do {digit =num%10; rev=(rev*10) +digit; num=num/10;


while(num!=0)

if(n==rev) {

true = print palindrome

false = print not palindrome

Step 4: End

Start

int num,n,digit,
n=num,rev=0;
Get num

do { digit=num
%10;rev=(rev*10)+digit;n
um=num/10;}V

while(nu
m!=0)

Print rev

if(n==rev)

Print Not Palindrome Print Palindrome

End

D. Pseudocode.

Start program {

Int num, n, digit, rev=0, n=num

Print Enter the number


Input number

do {digit =num%10; rev=(rev*10) +digit; num=num/10;

} while (num is not equal 0)

Print reverse num

If n is equal to rev

Print palindrome else not palindrome

E. Iteration table.

Iteration Variable num !=- 0 Action


1st num = 0 false The loop is terminated

3. A program to find the value of the polynomial design and evaluates the polynomial f(x) = a4 + x4 + a3
x3 + a2 x2+ a1 x + a0, for a given value of x and its coefficients using Horner’s method.

#include <iostream>

using namespace std;

int main()

int num1, num2, num3,counter = 0; int a[100];

int sum = 0;

cout << "Number 3.\n\n";

cout << "\nNote start with 0\n\n";

cout << "Enter the number coefficient >= " << endl;

cin >> num1;


cin >> num2;

cout << endl;

cout << "Enter the value of x: ";

cin >> num3;

cout << endl;

for(int i= 0; i <= num2;++i){

cout << "Enter a[" "]th coefficient: ";

cin >> a[i];

counter++;

sum+= a[i] * num3;

cout << endl;

cout << "Total value of the sum is " << sum << endl<< endl;

return 0;

A. Program plan.
Output:

Note start with 0

Enter the number coefficient >=

4
Enter the value of x:1 Total value of the sum is 10

Enter coefficient: 1

Enter coefficient: 2

Enter coefficient:3

Enter coefficient:4

B. Algorithm.

Step 1: Initialize int num1, num2, num3,counter = 0; int a[100]; int sum = 0;

Step 2: Get num1, num2, num3.

Step 3: for(int i= 0; i <= num2;++i) {cout << "Enter a[" "]th coefficient: ";

cin >> a[i]; counter++; sum+= a[i] * num3; }

Step 4: Print total sum.

Step 5: End.

Start
C. Flowchart

int num1, num2,


num3,counter = 0; int
a[100]; int sum = 0;

Get num1, num2,


num3
D. Pseudocode.

Start program

Initializing int num1, num2, num3,counter = 0; int a[100]; int sum = 0;

Get num1, num2, num3,

for(int i= 0; i <= num2;++i){

Get int a[i] counter incremented

Adding the total of sum+= a[i] * num3

} Print total sum;

End

F. Iteration table.

Iteration Variable i <= num2 Action


i=0 Not checked a[i] is increased by 1"Enter
the coefficient: " is printed.
1st i=1 True a[i] is increased by 2"Enter
the coefficient: " is printed
2nd i=2 True a[i] is increased by 3"Enter
the coefficient: " is printed
3rd i=3 True a[i] is increased by 4"Enter
the coefficient: " is printed
4th i=4 Flase The loop is terminated

4. A prime number is an integer greater than one and divisible only by itself and one. The first seven
prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all the prime numbers
between 1 and 100.

#include <iostream>

#include <cmath>

using namespace std;

int main()

cout << "Number 4.\n\n";


cout << "\n\t\t\tPrime numbers\n" << endl;

for(int i=2;i<=100;i++){

int counter =0;

for(int j=2;j<=sqrt(i);++j){

if (i%j==0)

counter=1;

if(counter==0)

cout <<i<< " ";

} cout << endl;

return 0;

A. Program Plan.

Int i= 2

Prime Numbers

Output:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

B. Algorithm.

Step 1: Print Prime Numbers


Step 2: for(int i=2;i<=100;i++) { int counter = 0;

for(int j=2;j<=sqrt(i);++j){

if (i%j==0)

counter=1; }

if(counter==0)

cout <<i<< " ";

Step 3: End
Start

C. Flowchart.

i<=100 increment

int counter =
0;

j<=sqrt(i increment
)

if (i
%j==0)

counter=1;

if(counte
r==0) Print <<i<< " "

End

D. Pseudocode.

Start program
Print Prime Numbers

for(int i=2;i<=100;i++) int counter =0;

for(int j=2;j<=sqrt(i);++j)

if (i%j==0) counter=1;

if(counter==0)

Print i “ ”

End

E. Iteration table.

Iteration Variable i<=100 Action

i =2 Not checked 2 is printed and i is


increased to 3
1st i =3 True 3 is printed and i is
increased to 5
2nd i =5 True 5 is printed and i is
increased to 7
3rd i =7 True 7 is printed and i is
increased to 11
4th i =11 True 11is printed and i is
increased to 13
5th i =13 True 13 is printed and i
is increased to 17
6th i =17 True 17 is printed and i
is increased to
19….
26th i = 101 False The loop is terminated

5. Write a program that counts the number of digits in an integer number. For
example; 23,498 has five digits.

#include <iostream>

using namespace std;


int main()

int num,temp, counter=0;

cout << "Number 5.\n\n";

cout << "\t Counting Numbers\n\n";

cout << "Enter the number: ";

cin >> num;

cout << endl;

temp = num;

while(temp != 0){

counter++;

temp /= 10;

cout << "Total digits in " << num << " is "<< counter << endl;

return 0;

A. Program Plan

Counting Numbers

Enter the number: 890

Output:

Total digits in 890 is 3.

B. Algorithm.
Step 1: Initialize num, temp, counter = 0;

Step 2: Print Enter the number:

Step 3: Get num

Step 4: temp = num

Step 5: while (temp != 0){

counter++; temp /= 10;

Step 6: Print Total digits (num) is (counter)

Step 7: End.

C. Flowchart.

Start

Int num, temp,


counter = 0;

Get num

Temp =
D. Pseudocode.

Start Program

int num,temp, counter=0;

Print Counting Numbers

Print Enter the number cin: num

temp = num

while (temp != 0){ counter++; temp /= 10; }


Print Total digits of num is counter.

End

. Iteration Table.

Iteration Variable temp != 0 Action


temp = num true num and counter is
printed

CODES:

You might also like