Share on Google+Share on Google+

Pedro Esteves
Java Pyramid of Numbers
3 Answer(s)      6 years and 2 months ago
Posted in : Java Beginners

Hi, I want to know how the code to print the pyramid below works. It uses nested for loops.

Pyramid:

                                          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
                          9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9

Ads

View Answers

February 11, 2011 at 12:41 PM


Java Pyramid of Numbers

class Pyramid{
    public static void main(String[] args){
     int x = 9;  
     for (int i = 1; i <= x; i++) {  
     for (int j = 1; j <= x - i; j++)  
     System.out.print("   ");  
     for (int k = i; k >= 1; k--)  
     System.out.print((k >=10) ?+ k : "  " + k);  
     for (int k = 2; k <=i; k++)  
     System.out.print((k>= 10) ?+ k : "  " + k);  
     System.out.println();  
    }  
  }
}


February 11, 2011 at 8:35 PM


Thank you. But I keep don't understanding how the spaces between numbers are working



February 11, 2012 at 12:47 AM


instead of using System.out.print with conditional, you could do this: this will allocate five spaces for the numbers.

class Pyramid{
    public static void main(String[] args){
     int x = 9;  
     for (int i = 1; i <= x; i++) {  
     for (int j = 1; j <= x - i; j++)  
     System.out.printf("%5s", "");  
     for (int k = i; k >= 1; k--)  
     System.out.printf("%5d", k);  
     for (int k = 2; k <=i; k++)  
     System.out.printf("%5d", k);  
     System.out.println();  
    }  
  }
}









Related Tutorials/Questions & Answers:
Java Pyramid of Numbers
Java Pyramid of Numbers  Hi, I want to know how the code to print the pyramid below works. It uses nested for loops. Pyramid: 1 2 1 2
Printing numbers in pyramid format - Java Beginners
Printing numbers in pyramid format  Q) Can you please tel me the code to print the numbers in the following format: 1... the following code: public class Pyramid{ public static void main (String
Advertisements
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8
Application for print pyramid of numbers
Application for print pyramid of numbers  What is the code for printing the numbers like 1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
pyramid
pyramid  hi frnds i need java code for pyramid. the pyramid look like this A BAB CBABC BAB
pyramid
pyramid  hi i need java code for pyramid. the pyramid look like this. A BAB CBABC BAB
PYRAMID - Java Beginners
PYRAMID  I want to know a java program that using For Loop which will display triangle or pyramid... The user must choose A, B, or C...: A (so it will display a Pyramid OR TRIANGLE) would you like to continue y
pyramid
pyramid  how to print 1 22 333 4444 55555   class Pyramid { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print(i
pyramid
pyramid  How to get the code for the following pattern * * * * * * * * * *   class Pyramid{ public static void main(String args[]){ for(int i=1;i<=4;i
pyramid
pyramid  How to get the code for the following pattern * * * * * * * * * *   class Pyramid{ public static void main(String args[]){ for(int i=1;i<=4;i
pyramid
pyramid  how to get the output like this A BB CCC DDDD   Hi Friend, Try the following code: class Pyramid { public static void main(String[] args) { for(char ch1='A';ch1<='D';ch1
how to add to numbers in java
how to add to numbers in java  how to add to numbers in java
defining numbers in Java Script
defining numbers in Java Script  Explain about defining numbers in Java Script
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
Perfect Numbers - Java Beginners
+ 2 + 3 Write a java program that finds and prints the three smallest perfect numbers. Use methods   Hi Friend, Try the following code: public
random numbers - Java Beginners
random numbers  write a program to accept 50 numbers and display 5 numbers randomly  Hi Friend, Try the following code: import...); System.out.println("Enter 10 numbers: "); for(int i=0;i<10;i
recursion numbers - Java Beginners
recursion numbers  I need to use recursion to test all values from 0 to 20 and see if they are contain in a 1-D array with values: 2,4,6,8,10,12,14,16,18,20. The results of all numbers from 0-20 will be printed
Generating random numbers in a range with Java
Generating random numbers in a range with Java  Generating random numbers in a range with Java
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
random numbers - Java Beginners
to display the random numbers, but not twice or more. I mean i need a number to be display once. This code allows some numbers to be displayed more than once. Hi... Scanner(System.in); System.out.println("Enter 10 numbers: "); for(int i=0;i<10;i
nmber printed in pyramid format
nmber printed in pyramid format  how to print this format using java 1 23 345 4567 56789
permutstion of numbers in java
permutstion of numbers in java  Is it possible to enter the number in a char so that permutation of a number can be acheived which will be in the form of string????? here is the coding i did...it worked really well when i
number pyramid
;class Pyramid{ public static void main(String args[]){ for(int i=1;i<=7...; class Pyramid{ public static void main(String args[]){ for(int i
Number Pyramid
for the following number pyramid: print.... So far I have this: print("import java.util.Scanner; class Pyramid { public... Pyramid { public static void main(String[] args) { Scanner input = new Scanner
EVEN NUMBERS - Java Interview Questions
EVEN NUMBERS  i want program of even numbers?i want source code plz reply?  Hi Friend, Try the following code: class EvenNumbers... counter = 0; System.out.println("Even Numbers are:" ); for (int i
Textbox allows only numbers in java wicket
Textbox allows only numbers in java wicket  Please provide me wicket code for text box that allows only numbers to type. Thank you
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads