7 Replies - 743 Views - Last Post: 08 August 2012 - 09:20 PM Rate Topic: -----

#1 akosiangelo12  Icon User is offline

  • New D.I.C Head

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

I have a problem about this pyramid of numbers

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
  • +

Replies To: I have a problem about this pyramid of numbers

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I have a problem about this pyramid of numbers

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....
Was This Post Helpful? 0
  • +
  • -

#3 SPorter  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: I have a problem about this pyramid of numbers

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 :)
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: I have a problem about this pyramid of numbers

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

Was This Post Helpful? 2
  • +
  • -

#5 SPorter  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: I have a problem about this pyramid of numbers

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

Was This Post Helpful? 1
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I have a problem about this pyramid of numbers

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.
Was This Post Helpful? 0
  • +
  • -

#7 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: I have a problem about this pyramid of numbers

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
Was This Post Helpful? 0
  • +
  • -

#8 akosiangelo12  Icon User is offline

  • New D.I.C Head

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

Re: I have a problem about this pyramid of numbers

Posted 08 August 2012 - 09:20 PM

thanks for the helpful comments,,now i already understand the logic of that problem..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1