help me--loop concepts | Newbie | | Join Date: Aug 2009
Posts: 1
| | |
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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: help me--loop concepts Quote:
Originally Posted by nishanjo 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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | re: help me--loop concepts Quote:
Originally Posted by nishanjo 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...
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by nishanjo 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 - for (int i=2;i<=4;i++)
-
{
-
system.out.println(i + ' ');
-
for (int j=1; j<=i;j++)
-
{
-
system.out.print(i*j + ' ');
-
}
-
}
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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: -
34
-
343635
-
35384136
-
36404448
-
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)
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by JosAH 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: -
34
-
343635
-
35384136
-
36404448
-
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. - system.out.println(i + " ");
and - system.out.println(i*j + " ");
Thanks anyway.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: help me--loop concepts Quote:
Originally Posted by OraMaster 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. - system.out.println(i + " ");
and - 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: -
int i= 21;
-
int j= 2;
-
System.out.println(i*j+"=42");
-
System.out.println("42="+i*j);
-
... prints 42=42 twice but the following: -
int i= 21;
-
int j= 21;
-
System.out.println(i+j+"=42");
-
System.out.println("42="+i+j);
-
... doesn't.
kind regards,
Jos
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by JosAH Sort of; the name of the class is 'System' in Java, not 'system'; Java is case sensitive. There are more pitfalls, while this: -
int i= 21;
-
int j= 2;
-
System.out.println(i*j+"=42");
-
System.out.println("42="+i*j);
-
... prints 42=42 twice but the following: -
int i= 21;
-
int j= 21;
-
System.out.println(i+j+"=42");
-
System.out.println("42="+i+j);
-
... 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
| | | re: help me--loop concepts
Your code produces -
2
-
2 4 3
-
3 6 9 4
-
4 8 12 16
-
with single quotes replaced by double quotes. Notice the 3 in the second line.
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by r035198x Your code produces -
2
-
2 4 3
-
3 6 9 4
-
4 8 12 16
-
with single quotes replaced by double quotes. Notice the 3 in the second line. How below code will produce output you mentioned? - for (int i=2;i<=4;i++)
-
{
-
system.out.println(i + " ");
-
for (int j=1; j<=i;j++)
-
{
-
system.out.print(i*j + " ");
-
}
-
}
I have tested it and it's giving right result.
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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.
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by r035198x 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: - public class Test {
-
public static void main(String a[]) {
-
for (int i=2;i<=4;i++)
-
{
-
System.out.println(i + " ");
-
for (int j=1; j<=i;j++)
-
{
-
System.out.print(i*j + " ");
-
}
-
}
-
}
-
}
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | re: help me--loop concepts
Same output -
2
-
2 4 3
-
3 6 9 4
-
4 8 12 16
-
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.
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by r035198x Same output -
2
-
2 4 3
-
3 6 9 4
-
4 8 12 16
-
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. - public class Test {
-
public static void main(String a[]) {
-
-
for (int i=2;i<=4;i++)
-
{
-
System.out.print(i + " ");
-
for (int j=1; j<=i;j++)
-
{
-
System.out.print(i*j + " ");
-
}
-
System.out.println();
-
}
-
}
-
}
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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)
|  | Member | | Join Date: Aug 2009 Location: Pune, India
Posts: 76
| | | re: help me--loop concepts Quote:
Originally Posted by JosAH 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,387 network members.
|