Wipro NLTH Pattern Making Question: 2 (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++ –
Table of Contents
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
C
C++
Java
C
Run
#include <stdio.h> 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; }
C++
Run
#include<bits/stdc++.h> using namespace std; 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++) { 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; }
Java
Run
class Main { public static void main (String[]args) { int count1 = 0, count2 = 0; int N = 2; 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 (); } } }
NOTE:- Please do comment the code in other languages 🙂
n=int(input())
l=1
c=0
k=n**2+1
t=4*n-1
p=0
for i in range(n):
for m in range(p):
print(” “,end=”)
for j in range(t):
if j%2==0:
if j<=t//2:
print(l,end='')
l=l+1
else:
print(k,end='')
k=k+1
c=c+1
else:
print("*",end="")
k=k-2*c+1
c=0
p=p+2
t=t-4
print( )
n=int(input())
temp=n
count=1
for i in range(n):
for j in range(0,i):
print(” “,end=”)
for k in range(temp):
print(count,end=”*”)
count+=1
count=count-temp
for l in range(temp,2*temp):
if l==2*temp-1:
print(temp*temp+count,end=”)
else:
print(temp*temp+count,end=”*”)
count+=1
print()
temp-=1
Python Code
#include
using namespace std;
int main(int argc, char *argv[])
{
int n,m=1;
std::cin >> n;
for (int i = n; i > 0; –i) {
int k=(n*(n+1)/2)+(i*(i-1)/2)+1;
for (int y = 0; y < n-i; ++y) {
std::cout << "\t";
}
for (int j = 1; j <= i*2; ++j) {
if(j<=i)
{
std::cout << m << "\t";
++m;
}
else
{
std::cout << k++ << "\t";
}
}
std::cout << std::endl;
}
return 0;
}
#include
#include
using namespace std;
int main() {
int n;
cin>>n;
int cnt=n,l=1,r=n*n+1;
while(cnt)
{
for(int i=0;i<n-cnt;i++)cout<<" "; //space
for(int i=l; i<l+cnt ;i++) // first n numbers
cout<<i<<"*";
for(int j=r ; j<r+cnt-1 ;j++) //last n numbers
cout<<j<<"*";
cout<<r+cnt-1<<endl;
l+=cnt;
r-=cnt-1;
cnt–;
}
return 0;
}
In Python:-
input1 = int(input(“Enter the range:”))
sum = 0
total = (input1*(input1+1))
count = total
for i in range(input1):
print(” “*(i),end=””)
for k in range(input1,i,-1):
sum += 1
print(sum,end=”*”)
count = total – sum
for j in range(input1,i,-1):
count += 1
if(j==i+1):
print(count,end=””)
else:
print(count,end=”*”)
print()
venuabi python code
n=int(input())
t=(n*(n+1))+1
z=1
space=0
for i in range(0,n):
y=(t-(n-i))
for j in range(space):
print(‘ ‘,end=”)
for k in range(1,(n+1)-i):
print(z,end=’*’)
z=z+1
for l in range(y,t):
if l==(t-1):
print(l)
break
print(l,end=’*’)
space+=2
t=y
# include
# include
void main()
{
int n,i,j,k,temp,count1,count2,count3=-1,temp2;
printf(“Enter number n “);
scanf (“%d”,&n);
count1=1;
count2=n*(n+1);
temp=2*n;
temp2=count2;
for(i=1;i<=n;i++)
{
for(j=2;j<=i;j++)
{
printf(" ");
}
for(k=1;k<=temp;k++)
{
if(k<=temp/2)
{
printf("%d*",count1);
count1++;
count3++;
if (k== temp/2) count2=count2-count3;
}
else
{
if(k==temp)
{
printf("%d",count2);
}
else
{
printf("%d*",count2);
}
temp2–;
count2++;
}
}
temp=temp-2;
count3=-1;
count2=temp2;
printf("\n");
}
getch();
}
n=int(input())
rval=1
m=n
k=1
for i in range(n):
lval=m*m+k
for j in range(i*2):
print(“-“,end=””)
for j in range((n-i)):
print(str(rval)+”*”,end=””)
rval=rval+1
for j in range(n-i):
if(j==(n-i-1)):
print(str(lval),end=””)
else:
print(str(lval)+”*”,end=””)
lval=lval+1
m=m-1
k=k+(n-i)
print()