Share

loop programming exercises in java, c++, python and javascript —06

loop programming exercises in java, c++, python and javascript

Java

int num=20;
	 for(int i=1;i<=5;i++){
		 for(int j=1;j<=num;j++){
			 System.out.print(" ");
		 }
		 num--;
		 for(int k=1;k<=i;k++){
			 System.out.print("* ");
		 }
		 System.out.println();
	 }

Python

py=1
for x in range(5,0,-1):
    for y in range(x,0,-1):
        print(" ",end="")
    print("*"*py)
    py+=2

C++

int num=20;
	 for(int i=1;i<=5;i++){
		 for(int j=1;j<=num;j++){
			cout<<" ";
		 }
		 num--;
		 for(int k=1;k<=i;k++){
			 cout<<"* ";
		 }
		 cout<<endl;
	 }

Javascript

let rows=5;
    for (let i = 0; i < rows; i++) {
        var output = " ";
        for (let j =0; j < rows - i; j++) {
             output += ' ';
         }
 
        for (let k = 0; k <= i; k++) {
             output += '* ';
        }
        console.log(output);  
     }