C/c++ programming
Expert: Zlatko - 6/20/2011
Questionrespected sir,
please kindly reply me to print the following pattern in c++ using
nested while structure.
123454321
1234 4321
123 321
12 21
1 1
AnswerHello jaspreet
How about I give you a hint, then you try to finish the program, and if you have trouble, show me your attempt and I will help you more.
Try this.
First make 2 while loops which are not nested. The first while loop will print the ascending numbers, and the second while loop will print the descending numbers.
This will give you the output
123454321
Then wrap the two while loops in an outer while loop. The outermost while loop will go from 2 to 5 and its counter will determine which numbers are to be replaced by spaces. Then the inner loops will print spaces when the inner loop counters are greater than the outer loop counter.
In general, when you don't know how to solve the problem, try to teach yourself by solving a simpler problem. In this case, start by trying to get
123454321
Then change your code and try to get
123454321
123454321
123454321
123454321
123454321
Then change your code and try to get the final result.
Good luck and best regards.
Zlatko