Thursday, June 18, 2020

Write a program to print the following pattern 3

Sample Input:

5

Sample Output:

1 
2*2 
3*3*3 
4*4*4*4 
5*5*5*5*5 
5*5*5*5*5 
4*4*4*4 
3*3*3 
2*2 
1

Sample Input:

4

Sample Output:

1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1

SOLUTION:

#include<iostream>
using namespace std;
void printPattern(int n){
int i,j,c=1;
  for(i = 1; i <= n; i++){
    for(j = 0; j < i-1; j++)
      cout<<c<<"*";
    cout<<c++<<endl;
  }
  c--;
    for(i = n; i >= 1; i--){
    for(j = i-1; j > 0; j--)
      cout<<c<<"*";
    cout<<c--<<endl;
  }
}
int main(){
  int n;
  cin>>n;
  printPattern(n);
}


No comments:

Post a Comment

horizontal ads