Rizal Technological University: (D) Pascal's Triangle, Height 8
Rizal Technological University: (D) Pascal's Triangle, Height 8
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
***********
*********
*******
*****
***
*
(b) a right triangle height = 5
A
AB
ABC
ABCD
ABCDE
(c.) a scalene triangle
1
121
12321
1234321
123454321
(d)
(e)
Tetrahedron, height = 6
#include <iostream>
int main()
char letter;
cout << "\n[A].Inverted Pyramid" << "\n" << "\n[B].Right Triangle" << "\n" << "\n[C].Scalene Triangle"
<< "\n" << "\n[D].Pascal Triangle"
if(letter == 'A'){
int height;
int h = 1, num;
value = 1;
for(int k=0;k<=i;k++){
value = value*(i-k)/(k+1);
}
} else if (letter == 'E'){
int i,j,k,l,n;
cout<<"\n";
cout<<"TETRAHEDRON TRIANGLE\n"<<endl;
cin>>n;
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.
A. Inverted Pyramid
B. Right Triangle
C. Scalene Triangle
D. Pascal Triangle
E. Tetrahedron
B. Algorithm.
Step 8: End
Start
C. Flowchart.
char letter;
Get letter
if(letter == decrement
'A')
For i >= 1
space <
height-i
increment
j <= 2*i-1
increment
End
End
D. Pseudocode.
Start Program.
Char letter;
Get letter;
End
E. Iteration Table.
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
B. Algorithm.
if(n==rev) {
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)
End
D. Pseudocode.
Start program {
If n is equal to rev
E. Iteration table.
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>
int main()
int sum = 0;
cout << "Enter the number coefficient >= " << endl;
counter++;
cout << "Total value of the sum is " << sum << endl<< endl;
return 0;
A. Program plan.
Output:
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 3: for(int i= 0; i <= num2;++i) {cout << "Enter a[" "]th coefficient: ";
Step 5: End.
Start
C. Flowchart
Start program
End
F. Iteration table.
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>
int main()
for(int i=2;i<=100;i++){
for(int j=2;j<=sqrt(i);++j){
if (i%j==0)
counter=1;
if(counter==0)
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.
for(int j=2;j<=sqrt(i);++j){
if (i%j==0)
counter=1; }
if(counter==0)
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 j=2;j<=sqrt(i);++j)
if (i%j==0) counter=1;
if(counter==0)
Print i “ ”
End
E. Iteration table.
5. Write a program that counts the number of digits in an integer number. For
example; 23,498 has five digits.
#include <iostream>
temp = num;
while(temp != 0){
counter++;
temp /= 10;
cout << "Total digits in " << num << " is "<< counter << endl;
return 0;
A. Program Plan
Counting Numbers
Output:
B. Algorithm.
Step 1: Initialize num, temp, counter = 0;
Step 7: End.
C. Flowchart.
Start
Get num
Temp =
D. Pseudocode.
Start Program
temp = num
End
. Iteration Table.
CODES: