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
Answers
Answer:
#include<iostream>
using namespace std;
int main(){
int n,num=1,i=1,space=0,k=1;
cin>>n;
int number=n;
for(i=0;i<n;i++)
{
for(int j=1;j<=space;j++)
{
cout<<"-";
}
for(int m=1;m<2*n-space;m++)
{
if(m%2==0)
cout<<"*";
else
cout<<num++;
}
cout<<"*";
for(int l=1;l<2*n-space;l++)
{
if(l%2==0)
cout<<"*";
else
{
cout<<k+number*number;
k++;
}
}
number--;
space=space+2;
cout<<endl;
}
return 0;
}
Explanation:
All condition satisfied
Answer:
#include<iostream>
using namespace std;
int main()
{
int n ;
cin>>n;
int space=0,k=1,p=n,t=n,m=1,f;
for(int i=0;i<n;i++){
for(int j=1;j<=t;j++)
m++;
t=t-1;
}m-=1;m*=2;
for(int i=0;i<n;i++){
for(int j=1;j<=space;j++)
{
cout<<"-";
}
for(int j=1;j<=p;j++){
cout<<k;k++;
cout<<"*"; }
if(i%2!=0){
f=m-p+1;
for(int j=1;j<=p;j++){
if(j!=1)
cout<<"*";
cout<<f;f++;m--;
}
}
else{
f=m-p+1;
for(int j=1;j<=p;j++){
if(j!=1)
cout<<"*";
cout<<f;f++;m--;
}
}
p=p-1;
space=space+2;
cout<<endl;
}
}