Connecting Tech Pros Worldwide Forums | Help | Site Map

help me--loop concepts

Newbie
 
Join Date: Aug 2009
Posts: 1
#1: Aug 2 '09
I am new in java..so plez some body help me to solve this loop question

Round Data
2 2 4
3 3 6 9
4 4 8 12 16
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 2 '09

re: help me--loop concepts


Quote:

Originally Posted by nishanjo View Post

I am new in java..so plez some body help me to solve this loop question

Round Data
2 2 4
3 3 6 9
4 4 8 12 16

Just a guess: the i-th line displays the numbers i 1*i 2*i ... i*i.

kind regards,

Jos
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#3: Aug 7 '09

re: help me--loop concepts


Quote:

Originally Posted by nishanjo View Post

I am new in java..so plez some body help me to solve this loop question

Round Data
2 2 4
3 3 6 9
4 4 8 12 16

There is no question there to answer...
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#4: Aug 7 '09

re: help me--loop concepts


Quote:

Originally Posted by nishanjo View Post

I am new in java..so plez some body help me to solve this loop question

Round Data
2 2 4
3 3 6 9
4 4 8 12 16

If you want output like
2 2 4
3 3 6 9
4 4 8 12 16
then below loop you need to execute in Java
Expand|Select|Wrap|Line Numbers
  1. for (int i=2;i<=4;i++)
  2. {
  3.  system.out.println(i + ' ');
  4.  for (int j=1; j<=i;j++)
  5.  {
  6.   system.out.print(i*j + ' ');
  7.  }
  8. }
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Aug 7 '09

re: help me--loop concepts


This doesn't compile because the class 'system' is not a known class. I can understand a littly typo (or a few of them) so I changed it to 'System'. The program fragment compiles now but gives me this as its output:

Expand|Select|Wrap|Line Numbers
  1. 34
  2. 343635
  3. 35384136
  4. 36404448
  5.  
This can be explained as well but I won't do it; please don't post such incorrect code (e.g. check it yourself before you post it) and please don't spoonfeed any OP by doing their homework. That is not helping, that is creating lazy people. Don't do that anymore.

kind regards,

Jos (moderator)
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#6: Aug 9 '09

re: help me--loop concepts


Quote:

Originally Posted by JosAH View Post

This doesn't compile because the class 'system' is not a known class. I can understand a littly typo (or a few of them) so I changed it to 'System'. The program fragment compiles now but gives me this as its output:

Expand|Select|Wrap|Line Numbers
  1. 34
  2. 343635
  3. 35384136
  4. 36404448
  5.  
This can be explained as well but I won't do it; please don't post such incorrect code (e.g. check it yourself before you post it) and please don't spoonfeed any OP by doing their homework. That is not helping, that is creating lazy people. Don't do that anymore.

kind regards,

Jos (moderator)

Hey Jos

I appriciate your reply. Actually i am Oracle PL/SQL guy don't know too much about Java. Actually i was trying to concatenate space with values of i and j inside System.out.println now i did confirm that was supposed to be double quotes instead of single quotes. Correct me if I am wrong.
for e.g.
Expand|Select|Wrap|Line Numbers
  1. system.out.println(i + " "); 
and
Expand|Select|Wrap|Line Numbers
  1. system.out.println(i*j + " "); 
Thanks anyway.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Aug 9 '09

re: help me--loop concepts


Quote:

Originally Posted by OraMaster View Post

Hey Jos

I appriciate your reply. Actually i am Oracle PL/SQL guy don't know too much about Java. Actually i was trying to concatenate space with values of i and j inside System.out.println now i did confirm that was supposed to be double quotes instead of single quotes. Correct me if I am wrong.
for e.g.

Expand|Select|Wrap|Line Numbers
  1. system.out.println(i + " "); 
and
Expand|Select|Wrap|Line Numbers
  1. system.out.println(i*j + " "); 
Thanks anyway.

Sort of; the name of the class is 'System' in Java, not 'system'; Java is case sensitive. There are more pitfalls, while this:

Expand|Select|Wrap|Line Numbers
  1. int i= 21;
  2. int j= 2;
  3. System.out.println(i*j+"=42");
  4. System.out.println("42="+i*j);
  5.  
... prints 42=42 twice but the following:


Expand|Select|Wrap|Line Numbers
  1. int i= 21;
  2. int j= 21;
  3. System.out.println(i+j+"=42");
  4. System.out.println("42="+i+j);
  5.  
... doesn't.

kind regards,

Jos
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#8: Aug 11 '09

re: help me--loop concepts


Quote:

Originally Posted by JosAH View Post

Sort of; the name of the class is 'System' in Java, not 'system'; Java is case sensitive. There are more pitfalls, while this:

Expand|Select|Wrap|Line Numbers
  1. int i= 21;
  2. int j= 2;
  3. System.out.println(i*j+"=42");
  4. System.out.println("42="+i*j);
  5.  
... prints 42=42 twice but the following:


Expand|Select|Wrap|Line Numbers
  1. int i= 21;
  2. int j= 21;
  3. System.out.println(i+j+"=42");
  4. System.out.println("42="+i+j);
  5.  
... doesn't.

kind regards,

Jos


Hi Jos,

I didn't get you. The code you written in your post is something unrelated with my last code. I did run my code and it's giving the desire output.

Regds,
Bhushan
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Aug 11 '09

re: help me--loop concepts


Your code produces
Expand|Select|Wrap|Line Numbers
  1. 2 4 3 
  2. 3 6 9 4 
  3. 4 8 12 16
  4.  
with single quotes replaced by double quotes. Notice the 3 in the second line.
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#10: Aug 11 '09

re: help me--loop concepts


Quote:

Originally Posted by r035198x View Post

Your code produces

Expand|Select|Wrap|Line Numbers
  1. 2 4 3 
  2. 3 6 9 4 
  3. 4 8 12 16
  4.  
with single quotes replaced by double quotes. Notice the 3 in the second line.

How below code will produce output you mentioned?

Expand|Select|Wrap|Line Numbers
  1. for (int i=2;i<=4;i++) 
  2.  system.out.println(i + " "); 
  3.  for (int j=1; j<=i;j++) 
  4.  { 
  5.   system.out.print(i*j + " "); 
  6.  } 
I have tested it and it's giving right result.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#11: Aug 11 '09

re: help me--loop concepts


Post the full code you have run and tested with then. You are still typing System as system which won't compile as has already been explained above. Show the full code that you are compiling and running successfully.
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#12: Aug 11 '09

re: help me--loop concepts


Quote:

Originally Posted by r035198x View Post

Post the full code you have run and tested with then. You are still typing System as system which won't compile as has already been explained above. Show the full code that you are compiling and running successfully.

Below is the complete code:

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.       public static void main(String a[]) {
  3.             for (int i=2;i<=4;i++) 
  4.             { 
  5.                     System.out.println(i + " "); 
  6.                     for (int j=1; j<=i;j++) 
  7.                     { 
  8.                             System.out.print(i*j + " "); 
  9.                     } 
  10.             } 
  11.       }
  12. }
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#13: Aug 11 '09

re: help me--loop concepts


Same output
Expand|Select|Wrap|Line Numbers
  1. 2 4 3 
  2. 3 6 9 4 
  3. 4 8 12 16 
  4.  
Obviously this is turning out to be a waste of time. The output I posted is what I get. You claim that it shows different output for you. Nothing more we can do about it but agree that we disagree.
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#14: Aug 11 '09

re: help me--loop concepts


Quote:

Originally Posted by r035198x View Post

Same output

Expand|Select|Wrap|Line Numbers
  1. 2 4 3 
  2. 3 6 9 4 
  3. 4 8 12 16 
  4.  
Obviously this is turning out to be a waste of time. The output I posted is what I get. You claim that it shows different output for you. Nothing more we can do about it but agree that we disagree.

Hi

I am sorry for posting incorrect code in my earlier post. Below is the right one.

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.       public static void main(String a[]) {
  3.  
  4.     for (int i=2;i<=4;i++) 
  5.             { 
  6.                     System.out.print(i + " "); 
  7.                     for (int j=1; j<=i;j++) 
  8.                     { 
  9.                             System.out.print(i*j + " "); 
  10.                     } 
  11.                     System.out.println();
  12.             }
  13.       }
  14. }
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#15: Aug 11 '09

re: help me--loop concepts


So after lots of effort you finally did the OPs homework; congratulations; now the OP didn't learn anything (you did) and the OP can turn it in as if it were his/hers; you helped the OP to cheat. Now what if his/her school turns to us (the forum) and accuses us for helping to cheat? We most certainly will point at you and show the entire thread to the teacher, we are not going to take the blame.

kind regards,

Jos (moderator)
OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#16: Aug 11 '09

re: help me--loop concepts


Quote:

Originally Posted by JosAH View Post

So after lots of effort you finally did the OPs homework; congratulations; now the OP didn't learn anything (you did) and the OP can turn it in as if it were his/hers; you helped the OP to cheat. Now what if his/her school turns to us (the forum) and accuses us for helping to cheat? We most certainly will point at you and show the entire thread to the teacher, we are not going to take the blame.

kind regards,

Jos (moderator)

Jos,

I don't think so.

Regds,
Bhushan
Reply

Tags
java, loop, nested loop