Java Pyramid 5 - Java Language Basics

Java examples for Language Basics:for

Introduction

how to generate pyramid or triangle like given below using for loop.

       
12345
1234
123
12
1
 

Demo Code

public class Main {
        public static void main(String[] args) {
                for(int i=5; i>0 ;i--){
                       /*from  w  w  w . j a  va2 s.  co  m*/
                        for(int j=0; j < i; j++){
                                System.out.print(j+1);
                        }
                        System.out.println("");
                }
 
        }
}
 

Result


Related Tutorials