Pattern Question (Trapezium)

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();
}
}

}

2 comments on “Pattern Question (Trapezium)”


  • Alok

    #include
    using namespace std;

    int main() {
    int n=5;

    int start=1, end=(n*(n+1)), space;

    for(int i=0; i<n; i++)
    {
    space=i*2;

    for(int j=0; j<space; j++)
    cout<<" ";
    for(int j=0; j<n-i; j++)
    {
    if(j==0)
    cout<<start;
    else
    cout<<"*"<<start;
    start++;
    }

    end=end-(n-i)+1;
    for(int j=0; j<n-i; j++)
    {
    cout<<"*"<<end++;
    }
    end=end-(n-i+1);
    cout<<endl;
    }
    return 0;
    }


  • Homemade

    public static void main(String[] args)
    {
    int n = scan.nextInt();
    int s1 = 1;
    int s2 = 2*((n*(n+1))/2);
    int s3 = n;
    int spa = 0;
    for(int i = 0;i<n;i++) {
    for(int j = 0;j<spa;j++)
    System.out.print(" ");
    spa+=2;
    for(int j = 0;j<s3;j++) {
    System.out.print(s1+"*");
    s1++;
    }
    int s4 = s2 – s3 +1;
    for(int j = 0;j<s3;j++) {
    System.out.print(s4+"");
    if(j!=s3-1)
    System.out.print("*");
    s4++;
    s2–;
    }
    s3–;
    System.out.println();
    }