Write a program in C++ to print Floyd’s Triangle.
Flowchart of the program in C++ to print Floyd’s Triangle
C++ code of the program to print Floyd’s Triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<iostream> using namespace std; int main() { int i,n,j,p,q; cout<<"Please Enter No of Rows : "; cin>>n; i=1; do { if(i%2==0) { p=1; q=0; } else { p=0; q=1; } j=1; do { if(j%2==0) { cout<<p; } else { cout<<q; } j++; } while(j<=i); i++; cout<<endl; } while(i<=n); } |
Output
Please Enter No of Rows: 5
1
01
101
0101
10101
Excercise
Find the possible mistakes in the following Shamilâs Flow Table of the program in C++ to print Floyd’s Triangle
Value of n | Loop 1 | If(i%2==0)
|
Loop 2 | If(j%2==0)
|
Which line will execute |
1 | I=1 | f | J=1 | f | 1,2,3,4,5,6,7,8,9,10,11,16,17,18,19,20,21,22,
23,24,26,27,28,29,30,31,32,33,34,35 |
2 | I=2 | T | J=2 | T | 1,2,3,4,5,6,7,8,9,10,11,16,17,18,19,20,21,22,
23,24,26,27,28,29,30,31,32,33,34,9,10,11,12, 13,14,15,21,22,23,24, 26,27,28,29,30,22,23,24,25, 28,29,30,31,32,33,34,35 |
3 | I=3 | F | J=3 | F | 1,2,3,4,5,6,7,8,9,10,11,16,17,18,19,20,21,22,
23,24,26,27,28,29,30,31,32,33,34,  9,10,11, 12,13,14,15,21,22,23,24, 26,27,28,29,30,22, 23,24,25, 28,29,30,31,32,33,34,   9,10,11,16,17,18,19,20,21,22,23,24,26,27,28, 29,30,22,23,24,25,28,29,30,22,23,24,26,28,29 ,30,31,32,33,34,35 |