Write a program to print a triangle.
Example:
* * * * ** * * ** * ** **
Approach:
Java
public class ReverseStarPrint {public static void main(String[] args) {int n = 5;int m = 5;for (int i = 0; i < n; i++) {for (int j = m; j > i; j--) {System.out.print("* ");}System.out.println();}}}
C++
#include <bits/stdc++.h>using namespace std;int main(){int n=5,m=5;for(int i=0;i<n;i++){for(int j=1;j<=m-i;j++)cout<<"* ";cout<<"\n";}return 0;}
No comments:
Post a Comment