• 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 many lines fo you want?");
	  int LineNum = input.nextInt();

	  if(LineNum < 0 || LineNum > 16);
	  System.out.println("Number of lines 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
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.

Link to comment
Share on other sites

  • 0

Since your pyramid looks fine, I'll assume you've got the column counting down, so...

Assuming you're on row N, then the logic should be:

Numbers to left of center: range from 2^0 to 2^N-2.

Center number: 2^N-1

Numbers to the right of center: 2^N-2 to 2^0

Link to comment
Share on other sites

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

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

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:):)

Link to comment
Share on other sites

  • 0

Barely 2 hours between posts and he bumps it again! This is also the 2nd time this user has basically requested us to do his homework

Link to comment
Share on other sites

  • 0
Looks like a homework project, you should of paid attention!

Should "have" paid attention ;) Maybe someone else didn't always listen in class :p

Link to comment
Share on other sites

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

    • No registered users viewing this page.