Share

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

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

Java

 int i,j,k,num=1;
	for (i=5; i>=1; i--) {
	     for (j=num;j >=0; j--) {
		  System.out.print(" ");
	     }
	     for (k=i; k>=1; k--) {
		  System.out.print("*");
	     }
	     num = num + 1;
	     System.out.print("\n");
       }

C++

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

Javascript

let height=5;
 for (var i = 1; i <= height; i++) {
   var row = '';

   for (var j = 1; j <= (i - 1); j++) {
     row += ' ';
   }

   for (var j = 1; j <= (height - i + 1); j++) {
     row += '*';
   }
       
   console.log(row);
 }