How to Print Sequence with single for loop?

Nov 27, 2011 at 4:12am
Hello hope all of you would be safe and sound.My problem is that dears i want to print following output so how could i make program for this.Single for loop should be used.And output will be that.

1
3
5
7
9
11
9
7
5
3
2
1

Plz any one could give me help how to make above output with single for loop.
Last edited on Nov 27, 2011 at 8:28am
Nov 27, 2011 at 5:20am
Is that '2' in the sequence intentional or typo?
Nov 27, 2011 at 8:18am
Dear i simply want to print following sequence on screen with single for loop.
1
3
5
7
9
11
9
7
5
3
2
1
Nov 27, 2011 at 8:44am
But the above sequence MUST have a logical pattern! Its fine till 7, 5, 3, but then this 2 appears. Does not satisfy its place in the sequence. I don't think its supposed to be there in the first place...
Also, a single for loop cannot do this. (By "for" loop, I'm assuming while and do-while as well, as their function is same)... You need two loops, one for ascending order of numbers, and the other for descending...
Nov 27, 2011 at 8:50am
If you really want it within 1 loop, you could have your loop variable "a" ascending 1,3,5 etc, and a variable within the loop ("b") that matches the loop variable "a" until 11. Using an "if" statement the direction of "b" could be switched when "a" reaches 11, and an if-statement could be used for the special case of "2" when descending.
Nov 27, 2011 at 8:58am
zugvogel, your logic may be correct, if you see her other posts, you'll see that she is a beginner, and using so many loops and counts is not a viable question. I feel that she has simply misunderstood the question, or the question allows use of a while loop as required, but only one for loop.
Topic archived. No new replies allowed.