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(); } } }

Ads

Related Tutorials/Questions & Answers:

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

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

Advertisements

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

Generating random numbers in a range with Java Generating random numbers in a range with 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

Textbox allows only numbers in java wicket Please provide me wicket code for text box that allows only numbers to type. Thank you

Add two big numbers - Java Beginners Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy

Prime numbers in Java between 1 and 100 Hi, How to display prime numbers in java between 1 and 100? Thanks Hi, Prime numbers... numbers in java between 1 and 100: package net.roseindia; /* * This program

How to add two numbers in a Java program? In Java you can easily add... numbers in Java it will return you int value as sum of two numbers. In real world... how to sum(add) two numbers in Java. Check more tutorials at: Java

Swapping of two numbers in java In this example we are going to describe swapping of two numbers in java without using the third number in java. We... values from the command prompt. The swapping of two numbers is based on simple

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

? The calculate the sum of two numbers using command line arguments in Java... sum = a+b; Check complete example code at Add Two Numbers in Java. Thanks...Sum of two numbers using command line arguments in Java Hi, How

how to write a program in java to print numbers in equalateral triangle the output must be 1 324 76589 Here is an example of pattern 1 2 3 4 5 6 7 8 9 Example: public class NumberTriangle{ public

Generating Random Numbers to Fill array. Java Beginner needing help! Hello all! I am new to this site, and Java programming. My problem is: Write a program that produces random permutations of the numbers 1 to 10. eg

ask user how many numbers to be inputted and determine the sum and highest number using an array in java ask user how many numbers to be inputted and determine the sum and highest number using an array in java

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

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

sorting numbers How to sort the numbers in ascending order import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers

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<

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

Java find prime numbers without using break statement In this tutorial, you will learn how to find the prime numbers without using break statement. You all are aware of Prime Numbers, these are the numbers which are either divided

Rational Numbers Write and fully test a class that represents rational numbers. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator

in understanding 'Java Script array of numbers'. For this we use Java... JavaScript array of numbers ... print the list of arrays of numbers.

Listing all even numbers between two numbers Hi, How to write code to list all the even numbers between two given numbers? Thanks Hi, You function will take 2 numbers and the find the even numbers between

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