Q-10: | Write a C/C++ program to print a triangle of asterisks(*) as follows (take number of lines from user as input):
* * * * * * * * * * * * * * * * |
---|
Using - C
#include<stdio.h> void main() { int n, i, j; printf("Enter the number of lines(rows): "); scanf("%d",&n); for(i=1; i <= n*2; i+=2){ for(j=1; j <= i; j++){ printf("* "); } printf("\n"); } }
OUTPUT
Enter the number of lines(rows): 4 * * * * * * * * * * * * * * * *
Using - C++
#include<iostream> using namespace std; int main() { int n; cout<< "Enter the number of lines(rows): "; cin>> n; for(int i=1; i <= n*2; i+=2){ for(int j=1; j <= i; j++){ cout<<"* "; } cout<< endl; } return 0; }
OUTPUT
Enter the number of lines(rows): 3 * * * * * * * * *