» » Print numbers in pyramid shape


  1. package com.instanceofjava;
  2.  
  3. public class Pyramidshape {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.   int num=10;
  8.  
  9.   for (int i = 1; i < num; i++) {
  10.  
  11.    for (int j = 1; j<=num-i;j++) {
  12.  
  13.     System.out.print(" ");
  14.    } 

  15.   for (int k = 1; k <= i; k++) {
  16.    System.out.print(""+k+" ");
  17.   } 

  18.   for (int l =i-1; l >0; l--) {
  19.    System.out.print(""+l+" ");
  20.   } 

  21.    System.out.println();
  22.   }
  23.  
  24. }



 Output:

  1.  1
  2.  1 2 1 
  3.  1 2 3 2 1
  4.  1 2 3 4 3 2 1

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu