Home Answers Viewqa Java-Beginners Java Pyramid of Numbers

 
 


Pedro Esteves
Java Pyramid of Numbers
3 Answer(s)      2 years and 4 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
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 Pages:
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
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
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
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
nmber printed in pyramid format
nmber printed in pyramid format  how to print this format using java 1 23 345 4567 56789
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 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
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 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
Numbers
Java NotesNumbers Two kinds of numbers. There are basically two kinds of numbers in Java and most other programming languages: binary integers (most... numbers, you will usually use decimal numbers in your Java source program
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link: Automorphic numbers Thanks
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
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
print numbers in traingle shape
print numbers in traingle shape  1 22 333 4444 i want output like this please help me   class Pyramid { public static void main(String[] args) { for(int i=1;i<=4;i++){ for(int j=1;j<
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
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
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
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
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
Random numbers - Introduction
Java NotesRandom numbers - Introduction When to use random numbers There are many types of programs that use random numbers. Game programs use them... numbers. You might want to use random numbers to change the appearance
odd numbers with loop
odd numbers with loop  get the odd numbers till 100 with for,while loop   Java find odd numbers: class OddNumbers { public static void main(String[] args) { for(int i=1;i<=100;i
Sum of first n numbers
Sum of first n numbers  i want a simple java program which will show the sum of first n numbers....   import java.util.*; public class...; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum
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
Divide 2 numbers
Divide 2 numbers  Write a java program to divide 2 numbers. Avoid division by zeor by catching the exception.   class Divide { public static void main(String[] args) { try{ int num1=8
loop code to print the Pyramid: 1 1234 12 123 123 12 1234 1
loop code to print the Pyramid: 1 1234 12 123 123 12 1234 1  Hi, I want to know how the code to print the pyramid below works. It uses nested for loops. Pyramid: 1 1234 12 123 123 12 1234 1
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
Applet for add two numbers
Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public...); add(text2); label3 = new Label("Sum of Two Numbers
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
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
adding two numbers - Java Beginners
information : http://www.roseindia.net/java/ Thanks
Java Find Automorphic numbers
Java Find Automorphic numbers In this section, you will learn how to find the automorphic numbers between 1 and 1000. Automorphic numbers are the numbers... of number 6 at the end. Here we are going to find the automorphic numbers between 1
Random Numbers - shuffling
Java NotesRandom Numbers - shuffling A standard approach to shuffling the elements of an array is to write some code as described below. As of Java 2... is described at the bottom. Shuffling: Random numbers without replacement
print 100 numbers using loops
print 100 numbers using loops  how to print from 1 to 100 using...); } } } Thanks   class Numbers { public satic void main...;a++) { System.out.println("val of a is ="+a); } } }   How java
Add Complex Numbers Java
How to Add Complex Numbers Java In this Java tutorial section, you will learn how to add complex Numbers in Java Programming Language. As you are already aware of Complex numbers. It is composed of two part - a real part and an imaginary
Finding all palindrome prime numbers - Java Beginners
Finding all palindrome prime numbers  How do i write a program to Find all palindrome prime numbers between two integers supplied as input (start and end points are excluded
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method