Java Generate Pyramid For a Given Number

 

Program to generate pyramid for a given number example in java.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* Program to generate pyramid for a given number example in java.
* @author W3schools360
*/
public class GeneratePyramidExample {
static void generatePyramid(int lines, int numDiff){
int y = 0;
for(int i=0; i<= lines; i++){
for(int j=1; j <= i ; j++){
System.out.print(y + "\t");
y = y + numDiff;
}
System.out.println("");
}
}
public static void main(String args[]){
//method call
generatePyramid(6, 3);
}
}
/** * Program to generate pyramid for a given number example in java. * @author W3schools360 */ public class GeneratePyramidExample { static void generatePyramid(int lines, int numDiff){ int y = 0; for(int i=0; i<= lines; i++){ for(int j=1; j <= i ; j++){ System.out.print(y + "\t"); y = y + numDiff; } System.out.println(""); } } public static void main(String args[]){ //method call generatePyramid(6, 3); } }
/**
 * Program to generate pyramid for a given number example in java.
 * @author W3schools360
 */
public class GeneratePyramidExample {	
	static void generatePyramid(int lines, int numDiff){
		 int y = 0;         
                 for(int i=0; i<= lines; i++){                
                   for(int j=1; j <= i ; j++){
                         System.out.print(y + "\t");
                         y = y + numDiff;
                   }                
                   System.out.println("");
               }
	}	
	
	public static void main(String args[]){ 
		//method call 
		generatePyramid(6, 3); 
	}
}

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
3 6
9 12 15
18 21 24 27
30 33 36 39 42
45 48 51 54 57 60
0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60
0	
3	6	
9	12	15	
18	21	24	27	
30	33	36	39	42	
45	48	51	54	57	60