Page 1 of 1

7 Replies - 1039 Views - Last Post: 08 August 2012 - 09:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288291&amp;s=35addcbf60f27edfcc484e6f4a1cf5e1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 akosiangelo12

Reputation: 0
• Posts: 3
• Joined: 08-August 12

Posted 08 August 2012 - 07:03 AM

I have a problem on how to make this output:
1112131415
78910
456
23
1

I created a code below, but the output is like this:
Output:
12345
6789
101112
1314
15

Code:
```int x,y,num=0;
for(x=1;x<=5;x++)
{
for(y=x;y<=5;y++)
{
num++;
System.out.print(num);
}
System.out.println();
}
```

I'm newbie in java programming, what should I do to make that output possible? thanks in advance.

This post has been edited by smohd: 08 August 2012 - 07:34 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Is This A Good Question/Topic? 0

#2 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

Posted 08 August 2012 - 07:47 AM

First you need to get the logic of the problem you want to solve. For here you have to know what relation those numbers have and what way they are changing.
Look at your first line and you will see number "1" precedes every number in the list. So probably you will need to loop from 1 to 5 and display the number preceded with 1.
Now look at other lines, what are you seeing? Numbers are displayed from 1 to 10 in reverse order but sequenced in each line. Now get that logic and start coding....

#3 SPorter

Reputation: 11
• Posts: 31
• Joined: 29-June 12

Posted 08 August 2012 - 10:22 AM

Really listen to what smohd said because that is how programming will always be. Breaking the problem down into pieces, finding the logic for it, and coding it.

Just to give you a slight hint to push you in the right direction. If you think of a for loop like a series of indexes, you will have a starting index of 0 and a finishing index of however many length of numbers you need. There are even indexes and odd indexes which you can place if/else statements on. Then, with the first string for example, try to figure out how you can use this knowledge to help you out. If 1 is preceding every number, is it an even index, an odd index?

I hope that gives you a push in the right direction

#4 Ryano121

• D.I.C Lover

Reputation: 1379
• Posts: 3,018
• Joined: 30-January 11

Posted 08 August 2012 - 10:27 AM

The problem is easier than that.

It just prints out the numbers from 15 to 1 in reverse order with some formatting.

This post has been edited by Ryano121: 08 August 2012 - 10:27 AM

#5 SPorter

Reputation: 11
• Posts: 31
• Joined: 29-June 12

Posted 08 August 2012 - 10:38 AM

Wow... I feel dumb that I didn't notice that haha! Good catch Ryano. Well, my same comment applies about thinking of a for loop as indexes, except in reverse. You are starting at the amount of numbers you need and finishing at 1

Edit: I felt I gave too much away to make it too easy rather than making him/her think, so I edited out the "giving it away" stuff

This post has been edited by SPorter: 08 August 2012 - 10:39 AM

#6 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

Posted 08 August 2012 - 11:52 AM

Nice catch Ryano121, I didnt see that the the first row was 11-15!
But the idea of solving programming problems is the same, think and break to see what you will do manually then implement it.

#7 Luckless

• </luck>

Reputation: 293
• Posts: 1,146
• Joined: 31-August 09

Posted 08 August 2012 - 12:10 PM

You've really almost got this. Try counting down from 15 instead of starting at 0. Think about how you'll get the numbers to print like this. The bigger numbers are to the right so you'll have to do some appending perhaps:

```...+ "13"+"14"+"15"

```

figure out how to automate this with your loops

#8 akosiangelo12

Reputation: 0
• Posts: 3
• Joined: 08-August 12