Wednesday, June 17, 2020

Write a program to print the trapezium pattern

Sample Input:

4

Sample Output:

1*2*3*4*17*18*19*20 
--5*6*7*14*15*16
----8*9*12*13
------10*11

Sample Input:

5

Sample Output:

1*2*3*4*5*26*27*28*29*30
--6*7*8*9*22*23*24*25
----10*11*12*19*20*21
------13*14*17*18
--------15*16

SOLUTION :

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

No comments:

Post a Comment

horizontal ads