Its a very common program asked to do during our course of Java.This program is very good to understand how loops works in Java.
Input:- 5
Ouptut:- *
**
***
****
*****
Sample Program:-
/*
This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.
*
**
***
****
*****
*/
public class JavaPyramid {
/**
* @param args
*/
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
// generate a new line
System.out.println("");
}
}
}
Output
*
**
***
****
*****
Enjoy Programming
No comments:
Post a Comment