• 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

Simple loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I am new to Java.

My requirement is I want to show the output like

1
2
3
4
5
6
7
8
9
10

What is the code for this?? I mean I would assign a value for 'n'. For eg n=10 means I need to break up that into 2 pieces (5 in each) and then a couple of white spaces then next 5.....

Can anybody help me out...???

Thanks in advance....
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Karthick Nath wrote:
Can anybody help me out...???


What have you tried so far? You'll need to show your own code to get help.
 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have tried that only. Please guide me.....
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks pretty good. What do you think is wrong with it? (I can see some minor things, but nothing major).

Also, UseCodeTags (← click) around your code. it helps readability. I've done it for you this time.
 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to the output to be as follows

1
2
3
4
5
space
6
7
8
9
10

If the 'i' value is 20 means the output should be

1
2
3
4
5
space
6
7
8
9
10
space
11
12
13
14
15
space
16
17
18
19
20

Please guid as I hope you understand my requirement......
 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried and achieved this of my own. Thanks for all your support who helped me out....



Output:
*********
1
2
3
4
5
Space
6
7
8
9
10
Space
11
12
13
14
15
Space
16
17
18
19
20
Space
21
22
23
24
25
Space
26
27
28
29
30
 
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know how to use more than one loop? Do you know how to use if statements?
You were not specific about your requirements. Do you mean printing a space character, or “space” or a blank line? Do you want a blank line after No 20?
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weclome to the Ranch I added code tags to your post. Always use them: doesn't it look better

Only ever use \n if somebody tells you they want the LF character. You simply write println(). Actually println("\n") will not give the output you say it does. it gives
1
2
3
4
5


6 … with two blank lines after the 5.
There is a better way to write the if statement, saving you writing println(i) twice. I shall let you find it.
 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Karthick.

println() stands for print line. So if you use System.out.println(), it will print whatever you have passed inside the parenthesis and will add add a Line Feed(LF) character at the end of the line.

You can also use System.out.print() This will print whatever you have inside the parenthesis, but it won't add the Line Feed character at the end.

So if you use,

The output is going to be:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quazi Irfan wrote:Hello Karthick.

println() stands for print line. So if you use System.out.println(), it will print whatever you have passed inside the parenthesis and will add add a Line Feed(LF) character at the end of the line.


I'd be a little careful on that. Not all OS's use the same line separator. Some use a LF. some use a LF/CR. it is possible there are others, but each JVM knows what is appropriate for the OS it is running on.
 
reply
    Bookmark Topic Watch Topic
  • New Topic