1 Replies - 7723 Views - Last Post: 02 November 2009 - 02:48 PM Rate Topic: -----

#1 erdemekinci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-November 09

How can i make pyramid at Java

Posted 02 November 2009 - 02:29 PM

hi. This is my first topic here. i was searching what i need on internet. bu i didn't find what exactly i want. so i decided to register and ask here.

i want to make a program that show number from 0 to 100 in pyramid form.

like this;

(forget dots. i put them to make pyramid shape. just spacebar is not working)
....................1
..............2....3....4
....... 5....6....7....8....9
.10...11..12...13..14..15...16
.
.
.
.
.

i know i am gonna use nested 'for' loops but i cant imagine algorithm that i need.

can anyone help me please?

Is This A Good Question/Topic? 0
  • +

Replies To: How can i make pyramid at Java

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: How can i make pyramid at Java

Posted 02 November 2009 - 02:48 PM

I have a much more complicated program sitting around I wrote a while ago (like a whiiiiiilllleeee ago) that does something similar, it will probably point you in the right direction:

import java.util.Scanner;

/**
 * A program to print a number of stars according to user input
 * @author Mike Magnus
 *
 */
public class StarProgram 
{
	public static void main(String[] args)
	{
		int user = 0;
		Scanner input =  new Scanner(System.in);
		
		//A while loop that simply validates input
		while (((user%2) == 0) || (user < 1) || (user > 19))
		{
			System.out.print("Please enter an odd integer:");
			user = input.nextInt();
		}
		for(int lines = 1;lines <= ((user/2)+1);lines++)
		{
			/*line*2 is because lines will only be half the value of user when the loop is
			 * at its maximum, so we want the logic to max out halfway.
			 * 
			 * -1 gives us a value that is still odd because doubling it gives us an even
			 * number that we don't want
			 * 
			 * user- gives us an inverse in our logic, that gets lower as lines gets higher.
			 * 
			 * the /2 is because we only need enough spaces for the first half of the lines
			 */
			for(int spaces = 0;spaces<(user-((lines*2)-1))/2;spaces++)
				System.out.print(" ");
			/*
			 * For the first lines loop, the number of stars will be directly proportional,
			 * giving us a simple comparison of
			 * lines*2 because we want it to max out once it is half way and this first lines
			 * loop is complete, and the -1 because we want an odd value still.
			 */
			for(int stars=0;stars<((lines*2)-1);stars++)
				System.out.print("*");
			System.out.print("\n");
		}
		for(int lines=(user/2)+2;lines<=user;lines++)
		{
			/*
			 * since lines will start one past half, it will now have to function in the opposite
			 * way to get a similar pattern in reverse, but it needed to be edited
			 * 
			 * (lines-1) compensates for (user/2) always being rounded down
			 */
			for(int spaces=0;spaces<(lines-1)-(user/2);spaces++)
				System.out.print(" ");
			/*
			 * Now the number of stars has to be proportional in a similar fashion to how
			 * spaces were, but they function in a slightly different manner.
			 * 
			 * (user-lines) will give us how many stars need to be on either side of the
			 * central star
			 * 
			 * the *2 will give us both sides of the central star
			 * 
			 * the +1 gives us back the central star
			 */
			for(int stars=0;stars<((user-lines)*2)+1;stars++)
				System.out.print("*");
			System.out.print("\n");
		}
		
	}

}


Now before anyone goes getting pissed at me for providing code without seeing code the code doesn't do what he needs it to and it has a lot of my comments in it, and it was written by me, and you may use portions, but you cannot copy my code.

This post has been edited by TriggaMike: 02 November 2009 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1