• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions related to loop  RSS feed

 
prakash china
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just want to know the different ways of answering the following questions.

�How to print 1 to 10 and 10 to 1 in a single loop?
�To print 12222
11222
11122
11112
�Print 1
2 3
4 5 6
7 8 9 10

�Print the highest prime number by giving �n� as input
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since these are "homework" questions, I am not sure if direct solutions will be allowed here.

If you post your answers first, this may be allowed... but even then, these are common homework questions, which may able to be searched on, by other students.

Henry
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you a hint for the first one.

Check out Math.abs, combined with the following formula:
f(x) = a * abs(b * x + c) + d

Find out what values of a, b, c and d you need to get the following:
f(0) = 1
f(1) = 2
f(2) = 3
f(3) = 4
f(4) = 5
f(5) = 6
f(6) = 7
f(7) = 8
f(8) = 9
f(9) = 10
f(10) = 10
f(11) = 9
f(12) = 8
f(13) = 7
f(14) = 6
f(15) = 5
f(16) = 4
f(17) = 3
f(18) = 2
f(19) = 1
[ September 28, 2008: Message edited by: Rob Prime ]
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that hint really help, Rob? I think he is looking for something much simpler.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Does that hint really help, Rob? I think he is looking for something much simpler.


That's the point. Is this a request looking for hints to do homework problems? Or is this some kind of "challenge" to find alternative solutions to the problem? This topic is worded as the later.

Rob is just posting an "exotic" alternative.... .... I am tempted to just lock this topic -- as there is no way to get into the "challenge" until the OP proves it is not homework.

Henry
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Silly question. Of course it's homework. What else could it be?
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us, Prakash China, what you have got so far.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Does that hint really help, Rob? I think he is looking for something much simpler.

Well since it has to be done in a single for loop, other than using recursion (which won't use a for loop at all) or an if statement (if i > X then count back), I fail to see an alternative.

All in all, I got it programmed with a loop body of two lines, one of which was just setting up the variable, the other printing it.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had it down to one line which printedbut I had to use two index ints in the "for."
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, that would also work. The assignment only says you need to count both up and down; it doesn't say how these two ranges should be printed. I assumed sequentially, but concurrently is of course also allowed. And then the solution (one index is enough, you can also subtract ) is much easier yes.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!