You are here:

C/c++ programming

Advertisement


Question
respected 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

Answer
Hello 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.