Thursday, August 16, 2018

How to create a pyramid of numbers in java?




package com.npp.test;

public class Test {

public static void main(String[] args) {

int n=10;
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++)
System.out.print(i>9?j<=n-i+1?"  ":i+"  ":j<=n-i+1?"  ":i+"   ");
}
  System.out.println();
}



  Instead of writing ternery operator we can write this also

if(i>9)
    System.out.print(j<=n-i+1?"  ":i+"  ");
else
    System.out.print(j<=n-i+1?"  ":i+"   ");





How to create a pyramid of numbers in java?



Share this

0 Comment to "How to create a pyramid of numbers in java?"

Post a Comment