InterviewSolution
Saved Bookmarks
| 1. |
Write a Java Program to display the pyramid as per the below design. |
|
Answer» * * * * * * * * * * * * * * * This can be achieved by using nested loops and calculatingly adding spaces and STARS as shown in the logic below: public class InterviewBitPyramid{ public STATIC void printPyramid(int N) { for (int i=0; i<n; i++){ // for number of rows for (int J=n-i; j>1; j--) { System.out.print(" "); //print space } //for number of columns for (int j=0; j<=i; j++ ) { System.out.print("* "); // print star } //end-line after every row System.out.println(); } } public static void main(String args[]){ printPyramid(5); //Print PYRAMID stars of 5 rows }}Output: * * * * * * * * * * * * * * * |
|