• 0

Java Number Pyramid =)


Question

Hello:

So I am suppose to build a number pyramid that takes a user input for the # of lines ( 1 -15 ), and that generates:

Example:

		   1
	 1	 2	 1
1	2	 4	 2	1

ETC...

I really messed mine up, with an example from the book.

import java.util.Scanner;
public class PowerTriangle{
	public static void main(String[]args){

	 Scanner input = new Scanner(System.in);

	  System.out.println("How lines fo you want?");
	  int LineNum = input.nextInt();

	  if(LineNum < 0 || LineNum > 16);
	  System.out.println("Numberines must be between 1 and 15");

	  for(int row = 1; row <= LineNum; row++){

		 for(int column = 1; column <= LineNum -row; column++)
			 System.out.print("   ");

		  for(int num = row; num >= 1; num--)
			 System.out.print((num >= 10) ? " " + num : "  " + num);

		  for(int num = 2; num <= row; num++)
			 System.out.print((num >= 10) ? " " + num : "  " + num);


  }
 }
}

I need to put "\n" but I dont know where, I got this as a result when I input 6:

How many lines fo you want?
6
Number of lines must be between 1 and 15
				 1			  2  1  2		   3  2  1  2  3		4  3  2  1  2  3  4	 5  4  3  2  1  2  3  4  5  6  5  4  3  2  1  2  3  4  5  6 (all on one line though not shown)
 ----jGRASP: operation complete.

So i need to fix my equations AND get them to format on their own lines.

P.S. I am new to this and am not majoring in computer things, but somehow this is required.

Link to comment
https://www.neowin.net/forum/topic/742056-java-number-pyramid/
Share on other sites

10 answers to this question

Recommended Posts

  • 0
						1
					 2  1  2
				  3  2  1  2  3
			   4  3  2  1  2  3  4
			5  4  3  2  1  2  3  4  5
		 6  5  4  3  2  1  2  3  4  5  6
	  7  6  5  4  3  2  1  2  3  4  5  6  7
   8  7  6  5  4  3  2  1  2  3  4  5  6  7  8

That is what is coming up.

						 1
					 1   2  1
				  1  2   4  2  1
			   1  2  4   8  4  2  1
			1  2  4  8  16  8  4  2  1
		 1  2  4  8 16  32 16  8  4  2  1
	  1  2  4  8 16 32  64 32 16  8  4  2  1
   1  2  4  8 16 32 64 128 64 32 16  8  4  2  1

That is what I want. 

I need to change the logic but I don?t know how.

  • 0
  Alladaskill said:
						1
					 2  1  2
				  3  2  1  2  3
			   4  3  2  1  2  3  4
			5  4  3  2  1  2  3  4  5
		 6  5  4  3  2  1  2  3  4  5  6
	  7  6  5  4  3  2  1  2  3  4  5  6  7
   8  7  6  5  4  3  2  1  2  3  4  5  6  7  8

That is what is coming up.

						 1
					 1   2  1
				  1  2   4  2  1
			   1  2  4   8  4  2  1
			1  2  4  8  16  8  4  2  1
		 1  2  4  8 16  32 16  8  4  2  1
	  1  2  4  8 16 32  64 32 16  8  4  2  1
   1  2  4  8 16 32 64 128 64 32 16  8  4  2  1

That is what I want. 

I need to change the logic but I don?t know how.

Hi, i think I got it some working

Look for this image

2uzecy0.jpg

Your whole program output can be splittted in two parts

Part 1 : First Triangle which increases from left to right

Part2 : Second Traingle which decreases from left to right

I have completed coding for first triangle that is Part 1

It is in C#..... Please bear with me

  Quote
namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

int temp =1;

for (int i = 6; i>0; i--)//for number of times the loop should run

{

//Assuming that we need spacing btwn two integers, so logically we need to double the spaces

for (int x = 1; x<i*2; x++)//for number of times the space should occur in a given loop

{

Console.Write("_

}

Console.Write( temp );

for (int j = i; j < 6; j++)

{

temp *= 2;

Console.Write(" " + temp);//Printng the numbers

}

Console.WriteLine();

temp = 1;

// you need to implement the reverse of this below

}

Console.ReadLine();

}

}

}

It will result in this form

//Please ignore the "_", i have put purposefully for your understanding

rhtmyc.jpg

Hope it helps:):)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That would be nice, if excel could refresh pivot tables without bugs. Frequently you refresh a pivot and all parameters get lost. Now this will happen automatically. Bravo MS!
    • If one could ever put all the crap of the world in one box, that would be Microsoft.
    • The most corrupt and vile CEO any company ever had ! The only company that makes an insane amount of money decade after decade but doesn't deliver on even barely passable quality to customers, screws over partners and treats employees like trash
    • PSA: Some other versions of Windows are losing support on October 14 too by Usama Jawad All of us here at Neowin likely know pretty well by now that Windows 10 is reaching end of support on October 14, 2025. You can extend support through paid and "free" means, but if you don't, you won't get any more security or feature updates following the aforementioned date. We also highlighted that the Long-Term Servicing Channel (LTSC) version Windows 10 22H2 is also reaching end of support on that date. Now, Microsoft has reminded customers that yet another variant of Windows is reaching end of life on October 14, 2025 too. On the Windows Release Health dashboard, Microsoft has published a reminder that the Enterprise, Education, and IoT SKUs of Windows 11, version 22H2 will hit end of support on October 14 as well. It is important to keep in mind that the Home and Pro variants of Windows 11, version 22H2 already reached end of life on October 8, 2024, and the extra year of lease on life will end for other SKUs within a few months too. Windows 11, version 22H2 for IoT, Enterprise, and Education was released on September 20, 2022, which means that they would have received just over three years of support by the time they "die", compared to the regular two years for Home and Pro. Customers on any of these versions should consider upgrading to version 23H2 or 24H2 as soon as possible, which have end of support dates of November 10, 2026 and October 12, 2027, respectively. Staying on a supported version of Windows is crucial as that allows you to receive regular security updates on your machine. If you're an IT admin, you should immediately begin planning a migration to a supported version of the operating system, and if you're an employee or someone using these versions of Windows in some other scenario, go to Settings > System > About and check out Windows Specifications > Version.
  • Recent Achievements

    • Dedicated
      Profit earned a badge
      Dedicated
    • One Month Later
      hhgygy earned a badge
      One Month Later
    • Week One Done
      hhgygy earned a badge
      Week One Done
    • One Year In
      NIKI77 earned a badge
      One Year In
    • Week One Done
      artistro08 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      635
    2. 2
      ATLien_0
      238
    3. 3
      Xenon
      167
    4. 4
      neufuse
      146
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!