Forums Register Login

Understanding logic on for loop

+Pie Number of slices to send: Send
Hi team,
can some one give me clue for printing this
*****
s****
ss***
sss**
ssss*

where s is space.
using for loop
since i am totally mad to achieve this, atlast i posted here
[ November 09, 2006: Message edited by: saravanakumar rajendran ]
+Pie Number of slices to send: Send
Ok, let's look at it:

Obviously, we need a loop of some sort to acheive this. Further investigation
of the output tells me we need 5 iterations.



Does this help you? if not, where are you stuck - code wise.

/Svend Rost
+Pie Number of slices to send: Send
Well I would use three for loops (although I'm sure there are alternative solutions). Consider the pattern looks like:

0 spaces : 5 stars
1 space : 4 stars
2 spaces : 3 stars
...

+Pie Number of slices to send: Send
for (int k=1;k<=5;k++)
{
for (int l=k+1;l<=5 ;l++ )
{
System.out.print("*");
}
System.out.println();

}

for this i am getting

****
***
**
* not as i expected one.
+Pie Number of slices to send: Send
 

Originally posted by saravanakumar rajendran:
for this i am getting

****
***
**
* not as i expected one.



Im using a single loop solution.

String s = "*****"
and at the end we want
s="....*" , where . = " "

Each loop we want to replace a * with a " ". Try to look in the API for
the String class for a usefull method..
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Does this help you?
+Pie Number of slices to send: Send
thanks a lot man.
+Pie Number of slices to send: Send
that api is technically advance, i need it for beginners level.
any way thanks a lot for your kind reply
+Pie Number of slices to send: Send
 

Originally posted by saravanakumar rajendran:
for (int k=1;k<=5;k++)
{
for (int l=k+1;l<=5 ;l++ )
{
System.out.print("*");
}
System.out.println();

}

for this i am getting

****
***
**
* not as i expected one.



Hi saravanakumar,

you didn't follow the advice Garrett gave you:

for (i = 0 -> totalNumberOfLines)
print i spaces
print (totalNumberOfLines - i) stars
print a new line character


You did this:

hint: you forgot to print the i spaces
+Pie Number of slices to send: Send
 

Originally posted by saravanakumar rajendran:
that api is technically advance, i need it for beginners level.
any way thanks a lot for your kind reply



Fair enough.. although I would suggest you to learn the API, or atleast
know how to look in it and search for usefull methods and information
about the different classes.

A more simple solution could be to operate with two strings. This solution
is based on the advice you got from Rowe.
+Pie Number of slices to send: Send
thanks guys, i got it now
+Pie Number of slices to send: Send
Hi to all.
I am new to JavaRanch. I am planning to take the SCJP exam (CX-310-055) by the end of Decemeber. I usually come here to see other people's problems, questions, etc. & try to solve them on my own. This helps me check my concepts in various fields.
I read the first post and made out the solution in just 2 minutes
Here is the code for my Diamond.java in (...)\language\looping\ directory:- (Sorry! The code is un-commented.)



I preferred to declare i, j and k outside the loops, so that variables don't have to be re-created during each iteration.
+Pie Number of slices to send: Send
for (int k=1;k<=5;k++)
{for(int z=6-k;z<5;z++)
{
System.out.print(" ");
}
for (int l=k+1;l<=5 ;l++ )
{
System.out.print("*");
}
System.out.println();
}
thanks for your kind reply, and this is my answer
+Pie Number of slices to send: Send
 

Originally posted by Abdul Rehman:
[QB]Hi to all.
I am new to JavaRanch. I am planning to take the SCJP exam (CX-310-055) by the end of Decemeber. I usually come here to see other people's problems, questions, etc. & try to solve them on my own. This helps me check my concepts in various fields.
I read the first post and made out the solution in just 2 minutes
Here is the code ....



Hi Abdul,

welcome to the Ranch!

Good job on solving the OPs (Original Posters) problem in 2 mins

General remark:

Please remember that this is the beginners forum and it's therefore important
that the OP'ers work out their solution (the techinical one, that is) by
themselves. Hints, ideas and pseudo code is to be prefeered over java code
in other words.

Now.. the OP seems to have solved the problem already, so posting code here
is okay... nevertheless I find it important to stress the importance of
the above


/Svend Rost
[ November 10, 2006: Message edited by: Svend Rost ]
+Pie Number of slices to send: Send
@Svend Rost

Thank you very much for your advice. I will take care in the future.
+Pie Number of slices to send: Send
 

I preferred to declare i, j and k outside the loops, so that variables don't have to be re-created during each iteration.


Suppose you have a loop like this:

for(int i = 0; i < 10; ++i)

If i was recreated during each interation, how would the ending condition ever be reached?

This helps me check my concepts in various fields.


Do some more reading about for-loops.
[ November 10, 2006: Message edited by: sven studde ]
+Pie Number of slices to send: Send
 

Originally posted by sven studde:

Suppose you have a loop like this:

for(int i = 0; i < 10; ++i)

If i was recreated during each interation, how would the ending condition ever be reached?



In the problem under discussion, we have nested looping. There is no problem with i, rather, j & (possibly) k would be re-created during each iteration of the outer loop. That's what I meant. One can declare i in the for-loop header; there's no problem with i.

Best regards,
Abdul Rehman.
+Pie Number of slices to send: Send
Please check this out.Quite simpler

String start = "*****";
StringBuffer buffer = new StringBuffer();
buffer.append(start);
System.out.println(buffer.toString());
for(int l=1;l<buffer.length();l++)
{
buffer = buffer.replace(l-1,l,"s");
System.out.println(buffer.toString());
}
Thanks,
Rahesh
+Pie Number of slices to send: Send
--->> this is your main
for (int i=0; i<totalNumberOfChar; i++) {
PrintChar(i, " ");
PrintChar(totalNumberOfChar-i, "*");
System.out.println();
}

--->> this is for PrintChar function or whatever you call this in java
Function: PrintChar
accepts: (int totalNumberOfChar, char charToPrint)

for (int i=0; i<totalNumberOfChar; i++) {
System.out.print(charToPrint);
}



i am also new in java would like to help too..
[ November 12, 2006: Message edited by: red rived ]
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3048 times.
Similar Threads
String class method
Static blocks
what does this means while((s+ss) == null) ?
confused about Scanner class
java program for sawtooth pattern
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:46:53.