Share

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

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

Java

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

Python

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

C++

int num=20;
	 for(int i=5;i>=1;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 = 4; k >= i; k--) {
           output += '* ';
       }
       console.log(output);  
   }