• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions related to loop

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that hint really help, Rob? I think he is looking for something much simpler.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Silly question. Of course it's homework. What else could it be?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us, Prakash China, what you have got so far.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic