Write nested loops to produce the pattern below | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Write nested loops to produce the pattern below

9 989 98789 9876789 987656789

7th Feb 2017, 9:36 AM
Dominic Mutiso
Dominic Mutiso - avatar
9 Answers
+ 1
int b; for (int i=0;i<length;++i) //here length = 5 { int b=9; for (int j=0;j<(length/2)-i;++j) //for blank spaces cout<<" "; for(j=0;j<2*i+1;++j) { if(j<(2*i+1)/2) cout<<b--; else cout<<b++; } } //i have written this code without a compile. but i believe it would run.
7th Feb 2017, 10:35 AM
GeekyShacklebolt
GeekyShacklebolt - avatar
+ 1
kindly post it here
7th Feb 2017, 10:50 AM
Dominic Mutiso
Dominic Mutiso - avatar
+ 1
not possible... its running in this solo learn compiler... i have checked it. come and check the running of code in the code posts area. search for most recent category in C++ "to print a pattern A numb. pyramid" and try to run My code.
7th Feb 2017, 11:29 AM
GeekyShacklebolt
GeekyShacklebolt - avatar
0
thanks let me try#CodeRunner
7th Feb 2017, 10:36 AM
Dominic Mutiso
Dominic Mutiso - avatar
0
well so sorry Dominic... I made a little mistake here in the code... i have corrected it and you may check the code on my profile... or let me give you the corrected one... #include <iostream> using namespace std; int main() { int b; int length=5; int j; for (int i=0;i<length;++i) //here length = 5 { int b=9; for (j=0;j<length-i;++j) //for blank spaces cout<<" "; for(j=0;j<2*i+1;++j) { if(j<(2*i+1)/2) cout<<b--; else cout<<b++; } cout<<"\n"; } return 0; } it is working!
7th Feb 2017, 10:49 AM
GeekyShacklebolt
GeekyShacklebolt - avatar
0
posted... : )
7th Feb 2017, 10:53 AM
GeekyShacklebolt
GeekyShacklebolt - avatar
0
aint working
7th Feb 2017, 11:17 AM
Dominic Mutiso
Dominic Mutiso - avatar
0
wow..kudoos
7th Feb 2017, 11:33 AM
Dominic Mutiso
Dominic Mutiso - avatar
21st Jul 2020, 1:04 AM
shubham kumar
shubham kumar - avatar