Nested for loop triangle problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 14868 Views - Last Post: 07 September 2009 - 03:28 AM Rate Topic: -----

#1 gabriel456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 09

Nested for loop triangle problem

Posted 04 September 2009 - 04:55 AM

hi guyz ..

somebody help me out with this problem..


i want to print this using nested for loop..


4321
321
21
1

tnx guyz..your help is very much appreciated

This post has been edited by gabriel456: 04 September 2009 - 04:56 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Nested for loop triangle problem

#2 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 04:59 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 gabriel456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 05:04 AM

heres the code that i wrote..

public class triangle {


public static void main(String[] args)
{


for (int row = 4; row >= 1; row-- )
{
for ( int column = 1; column <= row; column++ )
System.out.print(row);

System.out.println();
}
}
}



i dont have any idea how to make the number decrease..
im beginner in java

This post has been edited by gabriel456: 04 September 2009 - 05:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 05:22 AM

Now we can do something. Try to switch around your column loop so that it starts at row and decreases until it hits 1. Then print out column instead of row.

This post has been edited by syfran: 04 September 2009 - 05:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 gabriel456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 05:38 AM

hmm..i tried this code but nothings happen..

public class triangle {


public static void main(String[] args)
{


for (int row = 4; row >= 1; row-- )
{
for ( int column = 4; column <= 1; column-- )
System.out.print(column);

System.out.println();
}
}
}


Was This Post Helpful? 0
  • +
  • -

#6 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 05:40 AM

int column = 4; Is that what you want every line to start with? Shouldn't it have something to do with lets say, the row?
Was This Post Helpful? 0
  • +
  • -

#7 gabriel456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 05:52 AM

so should i put

Quote

column = 1;
??


itried it
public class exam3 {


public static void main(String[] args)
{


for (int row = 4; row >= 1; row-- )
{
for ( int column = 1; column <= row; column++ )
System.out.print(column);

System.out.println();
}
}
}



but the result is:

1234
123
12
1
Was This Post Helpful? 0
  • +
  • -

#8 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 05:58 AM

You want it to go from 4..1 so you need to reverse the column loop so it goes from row to 1;
Was This Post Helpful? 0
  • +
  • -

#9 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 541
  • Joined: 29-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 06:02 AM

I have absolutely grate idea for this one.
System.out.println("4321\n321\n21\n1");



That will work trust me :D :D

But seriously: Try looping the outer loop 4 times and then decrease the lower loop by one and display numbers from the highest num to lowest :D I guess you'll need to decrease that ass well but the starting point should be the outer loops highest number.

Oh try this for the inner loop
for(int column=row;column >=1;column--)


This post has been edited by Atspulgs: 04 September 2009 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#10 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 06:06 AM

View PostAtspulgs, on 4 Sep, 2009 - 05:02 AM, said:

I have absolutely grate idea for this one.
System.out.println("4321\n321\n21\n1");



That will work trust me :D :D

But seriously: Try looping the outer loop 4 times and then decrease the lower loop by one and display numbers from the highest num to lowest :D I guess you'll need to decrease that ass well but the starting point should be the outer loops highest number.

Oh try this for the inner loop
for(int column=row;column >=1;column--)



Hey, you just spoiled it. I've been slowly trying to let him get there by himself. :cry:
You ruined everything!!!!!
Was This Post Helpful? 0
  • +
  • -

#11 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 541
  • Joined: 29-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 06:10 AM

Oh sorry, i didnt felt that push from you :D
Was This Post Helpful? 0
  • +
  • -

#12 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 06:13 AM

lol, it was just so gentle, like the wind.

This post has been edited by syfran: 04 September 2009 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#13 gabriel456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 06:23 AM

public class exam3 {


public static void main(String[] args)
{


for (int row = 4; row >= 1; row--)
{
for(int column=row;column >=1;column--)
System.out.print(column);

System.out.println();
}
}
}
 




yeah..tnx atspulgs!.. :^:

tnx too syfran.. :D

i really appreciate your help!

my eyes are hurting.. weew,!

tnx again...
Was This Post Helpful? 0
  • +
  • -

#14 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 541
  • Joined: 29-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 06:27 AM

My apologies syfran, once again. I had no hostile intentions. I just took longer to write the post so two of you rushed in front of me in the end. besides this is what came in my mind after reading some first posts of yours here :D

class numberPrints
{
	public static void main(String[] args)
	{
		int[] nums = {4,3,2,1};

		for(int i=4;i>0;i--)
		{
			for(int j : nums)
				if(j<=i)System.out.print(j);
			System.out.println();
		}
	}
}



Sorry once again. I cant help the way i think :D
Was This Post Helpful? 0
  • +
  • -

#15 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Nested for loop triangle problem

Posted 04 September 2009 - 07:08 AM

No offense taken, No apologies needed. He got his code correct in the end and I'd like to think he knows why. So I'd say goals accomplished regardless of who did it :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2