Posted by:
Instanceofjava
Posted date:
September 13, 2015
/
concept and program
Program #1: java program to print pyramid of stars using for loop in below format
*
**
***
****
*****
******
*******
********
*********
**********
- package com.learnJavaOnline;
- public class PrintStarsFormat {
-
- public static void main(String[] args) {
-
- int numberOfStars=10;
-
- for (int row = 1; row <= 10; row++) {
-
- for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
- System.out.print("*");
- }
-
- System.out.println();
- }
-
- }
-
- }
Output:
- *
- **
- ***
- ****
- *****
- ******
- *******
- ********
- *********
- **********
Program #2: java program to print pyramid of stars using for loop in below format
**********
*********
********
*******
******
*****
****
***
**
*
- package com.learnJavaOnline;
- public class PrintStarsPyramid {
-
- public static void main(String[] args) {
-
- int numberOfStars=10;
-
- for(int i=numberOfStars; i>0 ;i--){
-
- for(int j=0; j < i; j++){
-
- System.out.print("*");
- }
-
- System.out.println("");
- }
-
- }
-
- }
Output:
- **********
- *********
- ********
- *******
- ******
- *****
- ****
- ***
- **
- *
Program #3 java program to print pyramid of stars using for loop in below format
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
- package com.learnJavaOnline;
- public class PrintStarsPyramid {
-
- public static void main(String[] args) {
-
- int numberOfStars=10;
-
- for(int i=1; i<= numberOfStars ;i++){
-
- for(int j=0; j < i; j++){
-
- System.out.print("*");
-
- }
-
- System.out.println("");
-
- }
-
- for(int i=numberOfStars; i>0 ;i--){
-
- for(int j=0; j < i; j++){
- System.out.print("*");
- }
-
- System.out.println("")}
-
- }
-
- }
Output:
- *
- **
- ***
- ****
- *****
- ******
- *******
- ********
- *********
- **********
- **********
- *********
- ********
- *******
- ******
- *****
- ****
- ***
- **
- *
Print Pascals triangle using java program