Inverted Star Triangle
Print an inverted right-angled triangle of stars with n rows (row i has n-i+1 stars).
Sample input
5
Sample output
*****
****
***
**
*
Solution
n = 5
for i in range(n, 0, -1):
print("*" * i)
const n = 5;
for (let i = n; i >= 1; i--) {
console.log("*".repeat(i));
}
public class Main {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
StringBuilder row = new StringBuilder();
for (int j = 0; j < i; j++) row.append("*");
System.out.println(row.toString());
}
}
}
fun main() {
val n = 5
for (i in n downTo 1) {
println("*".repeat(i))
}
}
let n = 5
for i in stride(from: n, through: 1, by: -1) {
print(String(repeating: "*", count: i))
}
void main() {
int n = 5;
for (int i = n; i >= 1; i--) {
print('*' * i);
}
}
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) cout << "*";
cout << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) printf("*");
printf("\n");
}
return 0;
}