Trapezium Pattern in C, Java

Here you will learn how to print the Trapezium Pattern in C language and Trapezium Pattern program in Java, Trapezium Pattern print in C++ – 

 

Ques. To print the trapezium pattern?

Please also post your code in the comments in different languages or same languages with short or better time complexity code.

If N = 4
1*2*3*4*17*18*19*20

5*6*7*14*15*16
8*9*12*13
10*11
If n = 5
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
If N = 2
1*2*5*6
3*4

 

In C

#include
int main(){
int n=5,num=1,i=1,space=0,k=1,number=n;
for(i=0;i<n;i++)
{
for(int j=1;j<=space;j++)
{

printf(“-“);

}
for(int m=1;m<2*n-space;m++)
{
if(m%2==0)
printf(“%s”,”*”);
else
printf(“%d”,num++);
}
printf(“%s”,”*”);
for(int l=1;l<2*n-space;l++)
{
if(l%2==0)
printf(“%s”,”*”);
else
{
printf(“%d”,k+number*number);
k++;
}
}
number–;

space=space+2;
printf(“\n”);
}
return 0;
}

In C++

#include
using namespace std;
int main(){
int n=4,num=1,i=1,space=0,k=1,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;
}

Trapezium Pattern program in Java

public class Pattern {

public static void main(String[] args) {

int count1=0,count2=0;
int N=4;
for(int i=N;i>=1;i–) {
for(int j=N;j>i;j–) System.out.print(” “);

for(int k=1;k<=i;k++) System.out.print(++count1+“*”);

for(int l=1;l<=i;l++) {
System.out.print(++count2+i*i);
if(l!=i) System.out.print(“*”);
}
System.out.println();
}
}

}

Summary
Review Date
Reviewed Item
Trapezium Pattern in C
Author Rating
51star1star1star1star1star