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