Jump to content

couple java questions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
jamesw

jamesw

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi

Can someone please just tell me, no links and so on. These surely are so easy for you, but not for me... : I wonder just what these two algorithms will print?

int x=1, y=0;
while(x<10) {
    x = y*y;
    y++;

}
System.out.println(x+" "+y);
I think this will print 4 and 5. ??

and this
for(int x=0; x<5; x++)
  for(int y=5; y>0; y--) {
if(x==y)
  break;
if(y%2!=0)
continue;
System.out.println(y);

}
I think this will print 2 ??

Edited by WingedPanther, 28 March 2009 - 04:44 PM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
For:

int x=1, y=0;

while(x<10) {

    x = y*y;

    y++;


}

System.out.println(x+" "+y);

x y
-- --
1 0
0 1
1 2
4 3
9 4
16 5

So the output is 16 5

For:

for(int x=0; x<5; x++)

  for(int y=5; y>0; y--) {

    if(x==y)

      break;

    if(y%2!=0)

      continue;

    System.out.println(y);

  }

x y
-- --
0 5
0 4 -> output 4
0 3
0 2 -> output 2
0 1
0 0
1 5
1 4 -> output 4
1 3
1 2 -> output 2
1 1
2 5
2 4 -> output 4
2 3
2 2 -> output 2
3 5
3 4 -> output 4
3 3
4 5
4 4 -> output 4
5 5
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog