Posted on by Kalkicode
Code Pattern

# Display Trapezium Pattern

In this article, we will discuss a program to display a trapezium pattern using C programming language. The trapezium pattern consists of numbers arranged in a specific format, creating a trapezium shape. We will provide an explanation of the problem, a detailed algorithm, pseudocode, and the expected output of the program.

## Problem Statement

The problem is to write a C program that displays a trapezium pattern based on a given size. The trapezium pattern is formed by arranging numbers in a specific way. The size of the trapezium determines the number of rows and the maximum number of digits in each row. Each row contains a sequence of numbers separated by asterisks (*) with spaces in between.

For example, if the given size is 3, the trapezium pattern would be:

```1*2*3*10*11*12
4*5*8*9
6*7
```

If the given size is 4, the trapezium pattern would be:

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

The goal is to generate the trapezium pattern based on the given size.

## Algorithm

Here is the algorithm to generate the trapezium pattern:

1. Define a function named `space` that takes the size as input and prints the required number of spaces.
2. Define a function named `trapezium_pattern` that takes the size as input and generates the trapezium pattern.
3. Inside the `trapezium_pattern` function, initialize variables `i`, `j`, `first`, `second`, and `back`.
4. Use a loop to iterate over each row of the trapezium pattern.
5. Inside the loop, call the `space` function to print the required number of spaces based on the current row index.
6. Use another loop to print the numbers in increasing order followed by asterisks (*) until the maximum number in that row is reached.
7. Decrement the `back` variable.
8. Use another loop to print the numbers in decreasing order followed by asterisks (*) until the initial number in that row is reached.
9. Adjust the `second` variable to the correct starting number for the next row.
10. Print a new line to move to the next row.
11. Call the `trapezium_pattern` function with different test cases from the `main` function.

## Pseudocode

Here is the pseudocode for the trapezium pattern program:

``````function space(size):
counter = 0
while counter < size:
print a space
increment counter

function trapezium_pattern(size):
print "Size: size"
i = 0
j = 0
first = 1
second = size * size + 1
back = size
while i < size:
space(i + i)
j = 0
while j < (size - i):
print first
if j != size - i:
print "*"
increment first
increment j
decrement back
j = size
while j > i:
print second
if j - 1 != i:
print "*"
increment second
decrement j
second = second - (size - i)
second = second - back
print a new line
increment i

main:
trapezium_pattern(3)
trapezium_pattern(4)
trapezium_pattern(7)```
```

## Code Explanation

The given C program contains two functions: `space` and `trapezium_pattern`.

The `space` function takes an input parameter `size` and prints a number of spaces based on the given size.

The `trapezium_pattern` function takes an input parameter `size` and generates the trapezium pattern. It uses nested loops to print the numbers and asterisks according to the required pattern.

In the `main` function, three test cases are provided to generate trapezium patterns of different sizes: 3, 4, and 7. The `trapezium_pattern` function is called with each test case to produce the corresponding trapezium pattern.

## Code Solution

``````//C Program
//Display Trapezium Pattern
#include <stdio.h>
#include <stdlib.h>

/*Include Space of given size*/
void space(int size) {

int counter = 0;

for (counter = 0; counter < size; counter++) {

printf(" ");
}
}

//Display the trapezium pattern of given size
void trapezium_pattern(int size) {

printf("\n Size : %d \n\n", size);
int i = 0;
int j = 0;
int first=1;
int second=(size*size)+1;
int back = size;

for (i = 0; i < size; i++) {

space(i+i);

for (j = 0; j < (size-i); ++j)
{
printf("%d",first );

if(j != size-i)
{
printf("*");
}
first++;
}
back--;

for (j = size; j > i ; --j)
{
printf("%d",second );

if(j-1!=i)
{
printf("*");
}
second++;
}
//back to initial position
second=second-(size-i);

//set next row first result
second=second-back;

printf("\n");

}
}

int main() {
//Test Cases
trapezium_pattern(3);
trapezium_pattern(4);
trapezium_pattern(7);
return 0;
}```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````/*
C++ Program
Display Trapezium Pattern
*/
#include<iostream>

using namespace std;
class MyPattern {
public:

/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {

cout << " ";
}
}
//Display the trapezium pattern of given size
void trapezium_pattern(int size) {
cout << "\n Size : " << size << " \n\n";
int i = 0;
int j = 0;
int first = 1;
int second = (size *size) + 1;
int back = size;
for (i = 0; i < size; i++) {
this->space(i + i);
for (j = 0; j < (size - i); ++j) {
cout << first;
if (j != size - i) {
cout << "*";
}
first++;
}
back--;
for (j = size; j > i; --j) {
cout << second;
if (j - 1 != i) {
cout << "*";
}
second++;
}
//back to initial position
second = second - (size - i);
//set next row first result
second = second - back;
cout << "\n";
}
}
};
int main() {
MyPattern obj =  MyPattern();
//Test Cases
obj.trapezium_pattern(3);
obj.trapezium_pattern(4);
obj.trapezium_pattern(7);
return 0;
}```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````/*
Java Program
Display Trapezium Pattern
*/

public class MyPattern {

/*Include Space of given size*/
public void space(int size) {

int counter = 0;

for (counter = 0; counter < size; counter++) {

System.out.print(" ");
}
}

//Display the trapezium pattern of given size
public void trapezium_pattern(int size) {

System.out.print("\n Size : "+size+" \n\n");
int i = 0;
int j = 0;
int first=1;
int second=(size*size)+1;
int back = size;

for (i = 0; i < size; i++) {

space(i+i);

for (j = 0; j < (size-i); ++j)
{
System.out.print(first );

if(j != size-i)
{
System.out.print("*");
}
first++;
}
back--;

for (j = size; j > i ; --j)
{
System.out.print(second );

if(j-1!=i)
{
System.out.print("*");
}
second++;
}
//back to initial position
second=second-(size-i);

//set next row first result
second=second-back;

System.out.print("\n");

}
}
public static void main(String[] args) {

MyPattern obj = new MyPattern();
//Test Cases
obj.trapezium_pattern(3);
obj.trapezium_pattern(4);
obj.trapezium_pattern(7);
}
}```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````/*
C# Program
Display Trapezium Pattern
*/
using System;

public class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
Console.Write(" ");
}
}
//Display the trapezium pattern of given size
public void trapezium_pattern(int size) {
Console.Write("\n Size : " + size + " \n\n");
int i = 0;
int j = 0;
int first = 1;
int second = (size * size) + 1;
int back = size;
for (i = 0; i < size; i++) {
space(i + i);
for (j = 0; j < (size - i); ++j) {
Console.Write(first);
if (j != size - i) {
Console.Write("*");
}
first++;
}
back--;
for (j = size; j > i; --j) {
Console.Write(second);
if (j - 1 != i) {
Console.Write("*");
}
second++;
}
//back to initial position
second = second - (size - i);
//set next row first result
second = second - back;
Console.Write("\n");
}
}
public static void Main(String[] args) {
MyPattern obj = new MyPattern();
obj.trapezium_pattern(3);
obj.trapezium_pattern(4);
obj.trapezium_pattern(7);
}
}```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````<?php
/*
Php Program
Display Trapezium Pattern
*/
class MyPattern {
/*Include Space of given size*/

public 	function space(\$size) {
\$counter = 0;
for (\$counter = 0; \$counter < \$size; \$counter++) {

echo(" ");
}
}
//Display the trapezium pattern of given size

public 	function trapezium_pattern(\$size) {
echo("\n Size : ". \$size ." \n\n");
\$i = 0;
\$j = 0;
\$first = 1;
\$second = (\$size *\$size) + 1;
\$back = \$size;
for (\$i = 0; \$i < \$size; \$i++) {
\$this->space(\$i + \$i);
for (\$j = 0; \$j < (\$size - \$i); ++\$j) {
echo(\$first);
if (\$j != \$size - \$i) {
echo("*");
}
\$first++;
}
\$back--;
for (\$j = \$size; \$j > \$i; --\$j) {
echo(\$second);
if (\$j - 1 != \$i) {
echo("*");
}
\$second++;
}
//back to initial position
\$second = \$second - (\$size - \$i);
//set next row first result
\$second = \$second - \$back;
echo("\n");
}
}
}

function main() {
\$obj = new MyPattern();
//Test Cases

\$obj->trapezium_pattern(3);
\$obj->trapezium_pattern(4);
\$obj->trapezium_pattern(7);

}
main();```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````/*
Node Js Program
Display Trapezium Pattern
*/
class MyPattern {
/*Include Space of given size*/
space(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {

process.stdout.write(" ");
}
}

//Display the trapezium pattern of given size
trapezium_pattern(size) {
process.stdout.write("\n Size : " + size + " \n\n");
var i = 0;
var j = 0;
var first = 1;
var second = (size *size) + 1;
var back = size;
for (i = 0; i < size; i++) {
this.space(i + i);
for (j = 0; j < (size - i); ++j) {
process.stdout.write(""+first);
if (j != size - i) {
process.stdout.write("*");
}
first++;
}
back--;
for (j = size; j > i; --j) {
process.stdout.write(""+second);
if (j - 1 != i) {
process.stdout.write("*");
}
second++;
}

//back to initial position
second = second - (size - i);
//set next row first result
second = second - back;
process.stdout.write("\n");
}
}
}

function main(args) {
var obj = new MyPattern();
//Test Cases
obj.trapezium_pattern(3);
obj.trapezium_pattern(4);
obj.trapezium_pattern(7);
}

main();```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````#   Python 3 Program
#   Display Trapezium Pattern

class MyPattern :
# Include Space of given size

def space(self, size) :
counter = 0
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1

# Display the trapezium pattern of given size
def trapezium_pattern(self, size) :
print("\n Size : ", size ," \n\n", end = "")
i = 0
j = 0
first = 1
second = (size * size) + 1
back = size
i = 0
while (i < size) :
self.space(i + i)
j = 0
while (j < (size - i)) :
print(first, end = "")
if (j != size - i) :
print("*", end = "")

first += 1
j += 1

back -= 1
j = size
while (j > i) :
print(second, end = "")
if (j - 1 != i) :
print("*", end = "")

second += 1
j -= 1

# back to initial position
second = second - (size - i)
# set next row first result
second = second - back
print("\n", end = "")
i += 1

def main() :
obj = MyPattern()
obj.trapezium_pattern(3)
obj.trapezium_pattern(4)
obj.trapezium_pattern(7)

if __name__ == "__main__":
main()```
```

#### Output

`````` Size :  3

1*2*3*10*11*12
4*5*8*9
6*7

Size :  4

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

Size :  7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````#   Ruby Program
#   Display Trapezium Pattern

class MyPattern
# Include Space of given size
def space(size)
counter = 0
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
# Display the trapezium pattern of given size
def trapezium_pattern(size)
print("\n Size  :", size ," \n\n")
i = 0
j = 0
first = 1
second = (size * size) + 1
back = size
i = 0
while (i < size)
self.space(i + i)
j = 0
while (j < (size - i))
print(first)
if (j != size - i)
print("*")
end
first += 1
j += 1
end
back -= 1
j = size
while (j > i)
print(second)
if (j - 1 != i)
print("*")
end
second += 1
j -= 1
end
# back to initial position
second = second - (size - i)
# set next row first result
second = second - back
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
obj.trapezium_pattern(3)
obj.trapezium_pattern(4)
obj.trapezium_pattern(7)
end
main()```
```

#### Output

`````` Size  :3

1*2*3*10*11*12
4*5*8*9
6*7

Size  :4

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

Size  :7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29
``````
``````/*
Scala Program
Display Trapezium Pattern
*/
class MyPattern {
/*Include Space of given size*/
def space(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size) {
print(" ");
counter += 1;
}
}
//Display the trapezium pattern of given size
def trapezium_pattern(size: Int): Unit = {
print("\n Size : " + size + " \n\n");
var i: Int = 0;
var j: Int = 0;
var first: Int = 1;
var second: Int = (size * size) + 1;
var back: Int = size;
i = 0;
while (i < size) {
this.space(i + i);
j = 0;
while (j < (size - i)) {
print(first);

if (j != size - i) {
print("*");
}
first += 1;
j += 1;
}
back -= 1;
j = size;
while (j > i) {
print(second);

if (j - 1 != i) {
print("*");
}
second += 1;
j -= 1;
}
//back to initial position
second = second - (size - i);

//set next row first result
second = second - back;
print("\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyPattern = new MyPattern();
obj.trapezium_pattern(3);
obj.trapezium_pattern(4);
obj.trapezium_pattern(7);
}
}```
```

#### Output

`````` Size : 3

1*2*3*10*11*12
4*5*8*9
6*7

Size : 4

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

Size : 7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````
``````/*
Swift Program
Display Trapezium Pattern
*/
class MyPattern {
/*Include Space of given size*/
func space(_ size: Int) {
var counter = 0;
counter = 0;
while (counter < size) {
print(" ", terminator: "");
counter += 1;
}
}
//Display the trapezium pattern of given size
func trapezium_pattern(_ size: Int) {
print("\n Size : ", size ," \n\n", terminator: "");
var i = 0;
var j = 0;
var first = 1;
var second = (size * size) + 1;
var back = size;
i = 0;
while (i < size) {
self.space(i + i);
j = 0;
while (j < (size - i)) {
print(first, terminator: "");
if (j != size - i) {
print("*", terminator: "");
}
first += 1;
j += 1;
}
back -= 1;
j = size;
while (j > i) {
print(second, terminator: "");
if (j - 1 != i) {
print("*", terminator: "");
}
second += 1;
j -= 1;
}
//back to initial position
second = second - (size - i);
//set next row first result
second = second - back;
print("\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj = MyPattern();
obj.trapezium_pattern(3);
obj.trapezium_pattern(4);
obj.trapezium_pattern(7);
}
main();```
```

#### Output

`````` Size :  3

1*2*3*10*11*12
4*5*8*9
6*7

Size :  4

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

Size :  7

1*2*3*4*5*6*7*50*51*52*53*54*55*56
8*9*10*11*12*13*44*45*46*47*48*49
14*15*16*17*18*39*40*41*42*43
19*20*21*22*35*36*37*38
23*24*25*32*33*34
26*27*30*31
28*29``````

## Time Complexity

The time complexity of this program is O(n^2), where n is the given size. This is because we have two nested loops: one loop runs from 0 to n, and the other loop runs from n to 0. As the size increases, the number of iterations in each loop increases quadratically. Therefore, the overall time complexity is O(n^2).

The space complexity of this program is O(1) because it does not use any additional data structures or dynamically allocate memory. It only uses a few variables to store intermediate values.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post