Posted on by Kalkicode
Code Pattern

# Print isosceles trapezoid shape

In this article, we will discuss how to print an isosceles trapezoid shape using a programming language. We will provide an explanation of the problem, present a suitable example, describe the algorithm and pseudocode, and analyze the time complexity of the code. The code provided is written in C language, but the concepts can be applied to other programming languages as well.

## Problem Statement

The problem is to write a program that prints an isosceles trapezoid shape using asterisk (*) symbols. The size of the trapezoid is provided as input, and the program should generate the corresponding pattern. An isosceles trapezoid is a geometric shape with two parallel sides of equal length, and the remaining two sides are of different lengths. The longer parallel sides are horizontal, while the shorter sides are slanted.

## Example

Let's consider an example to illustrate the problem. We have a size of 4. The expected output is:

```       * * * *
* * * * *
* * * * * *
* * * * * * *
```
Here, the trapezoid has four rows, and each row consists of asterisks (*) arranged in a specific pattern.

## Algorithm and Pseudocode

The algorithm to print the isosceles trapezoid shape involves two main steps: printing spaces and printing asterisks. Here is the pseudocode for the algorithm:

``````function include_space(size):
for i from 0 to size-1:
print " "

function include_star(size):
for i from 0 to size-1:
print "* "

function isosceles_trapezoid(size):
for i from 0 to size-1:
include_space(size - i)
include_star(size + i)
print new line

main:
isosceles_trapezoid(4)
isosceles_trapezoid(7)
isosceles_trapezoid(10)``````

## Code Solution

Here given code implementation process.

``````//C Program
//Print isosceles trapezoid shape
#include <stdio.h>

//include space character in given size
void include_space(int size)
{
for (int i = 0; i < size; ++i)
{
//include symbol
printf(" ");
}
}
//include star symbol in given size
void include_star(int size)
{
for (int i = 0; i < size; ++i)
{
//include symbol
printf("* ");
}
}
//Print isosceles trapezoid star pattern of given size
void isosceles_trapezoid(int size)
{
printf("\n Size : %d  \n\n", size);
//Loop controlling variable
int i = 0;
//Loop, which is execute row operations
for (i = 0; i < size; ++i)
{
include_space(size - i);
include_star(size + i);
printf("\n");
}
}
int main()
{
//Simple test
isosceles_trapezoid(4);
isosceles_trapezoid(7);
isosceles_trapezoid(10);
return 0;
}``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````// Java program
// Print isosceles trapezoid shape
class MyPattern
{
//include space character in given size
public void include_space(int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" ");
}
}
//include star symbol in given size
public void include_star(int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("* ");
}
}
//Print isosceles trapezoid star pattern of given size
public void isosceles_trapezoid(int size)
{
System.out.print("\n Size : " + size + " \n\n");
//Loop controlling variable
int i = 0;
//Loop, which is execute row operations
for (i = 0; i < size; ++i)
{
include_space(size - i);
include_star(size + i);
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
obj.isosceles_trapezoid(4);
obj.isosceles_trapezoid(7);
obj.isosceles_trapezoid(10);
}
}``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ program
// Print isosceles trapezoid shape
class MyPattern
{
public:
//include space character in given size
void include_space(int size)
{
for (int i = 0; i < size; ++i)
{
cout << " ";
}
}
//include star symbol in given size
void include_star(int size)
{
for (int i = 0; i < size; ++i)
{
cout << "* ";
}
}
//Print isosceles trapezoid star pattern of given size
void isosceles_trapezoid(int size)
{
cout << "\n Size : " << size << " \n\n";
//Loop controlling variable
int i = 0;
//Loop, which is execute row operations
for (i = 0; i < size; ++i)
{
this->include_space(size - i);
this->include_star(size + i);
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
obj.isosceles_trapezoid(4);
obj.isosceles_trapezoid(7);
obj.isosceles_trapezoid(10);
return 0;
}``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````//Include namespace system
using System;
// C# program
// Print isosceles trapezoid shape
class MyPattern
{
//include space character in given size
public void include_space(int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" ");
}
}
//include star symbol in given size
public void include_star(int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("* ");
}
}
//Print isosceles trapezoid star pattern of given size
public void isosceles_trapezoid(int size)
{
Console.Write("\n Size : " + size + " \n\n");
//Loop controlling variable
int i = 0;
//Loop, which is execute row operations
for (i = 0; i < size; ++i)
{
include_space(size - i);
include_star(size + i);
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
obj.isosceles_trapezoid(4);
obj.isosceles_trapezoid(7);
obj.isosceles_trapezoid(10);
}
}``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````<?php
// Php program
// Print isosceles trapezoid shape
class MyPattern
{
//include space character in given size
public	function include_space(\$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ";
}
}
//include star symbol in given size
public	function include_star(\$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "* ";
}
}
//Print isosceles trapezoid star pattern of given size
public	function isosceles_trapezoid(\$size)
{
echo "\n Size : ". \$size ." \n\n";
//Loop controlling variable
\$i = 0;
//Loop, which is execute row operations
for (\$i = 0; \$i < \$size; ++\$i)
{
\$this->include_space(\$size - \$i);
\$this->include_star(\$size + \$i);
echo "\n";
}
}
}

function main()
{
\$obj = new MyPattern();
\$obj->isosceles_trapezoid(4);
\$obj->isosceles_trapezoid(7);
\$obj->isosceles_trapezoid(10);
}
main();``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````// Node Js program
// Print isosceles trapezoid shape
class MyPattern
{
//include space character in given size
include_space(size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" ");
}
}
//include star symbol in given size
include_star(size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("* ");
}
}
//Print isosceles trapezoid star pattern of given size
isosceles_trapezoid(size)
{
process.stdout.write("\n Size : " + size + " \n\n");
//Loop controlling variable
var i = 0;
//Loop, which is execute row operations
for (i = 0; i < size; ++i)
{
this.include_space(size - i);
this.include_star(size + i);
process.stdout.write("\n");
}
}
}

function main()
{
var obj = new MyPattern();
obj.isosceles_trapezoid(4);
obj.isosceles_trapezoid(7);
obj.isosceles_trapezoid(10);
}
main();``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````#  Python 3 program
#  Print isosceles trapezoid shape
class MyPattern :
# include space character in given size
def include_space(self, size) :
i = 0
while (i < size) :
print(" ", end = "")
i += 1

# include star symbol in given size
def include_star(self, size) :
i = 0
while (i < size) :
print("* ", end = "")
i += 1

# Print isosceles trapezoid star pattern of given size
def isosceles_trapezoid(self, size) :
print("\n Size : ", size ," \n\n", end = "")
# Loop controlling variable
i = 0
# Loop, which is execute row operations
i = 0
while (i < size) :
self.include_space(size - i)
self.include_star(size + i)
print("\n", end = "")
i += 1

def main() :
obj = MyPattern()
obj.isosceles_trapezoid(4)
obj.isosceles_trapezoid(7)
obj.isosceles_trapezoid(10)

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

#### Output

`````` Size :  4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size :  7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size :  10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````#  Ruby program
#  Print isosceles trapezoid shape
class MyPattern

# include space character in given size
def include_space(size)

i = 0
while (i < size)

print(" ")
i += 1
end
end
# include star symbol in given size
def include_star(size)

i = 0
while (i < size)

print("* ")
i += 1
end
end
# Print isosceles trapezoid star pattern of given size
def isosceles_trapezoid(size)

print("\n Size : ", size ," \n\n")
# Loop controlling variable
i = 0
# Loop, which is execute row operations
i = 0
while (i < size)

self.include_space(size - i)
self.include_star(size + i)
print("\n")
i += 1
end
end
end
def main()

obj = MyPattern.new()
obj.isosceles_trapezoid(4)
obj.isosceles_trapezoid(7)
obj.isosceles_trapezoid(10)
end
main()``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
``````
``````// Scala program
// Print isosceles trapezoid shape
class MyPattern
{
//include space character in given size
def include_space(size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" ");
i += 1;
}
}
//include star symbol in given size
def include_star(size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("* ");
i += 1;
}
}
//Print isosceles trapezoid star pattern of given size
def isosceles_trapezoid(size: Int): Unit = {
print("\n Size : " + size + " \n\n");
//Loop controlling variable
var i: Int = 0;
//Loop, which is execute row operations
i = 0;
while (i < size)
{
include_space(size - i);
include_star(size + i);
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
obj.isosceles_trapezoid(4);
obj.isosceles_trapezoid(7);
obj.isosceles_trapezoid(10);
}
}``````

#### Output

`````` Size : 4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size : 7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size : 10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````
``````// Swift program
// Print isosceles trapezoid shape
class MyPattern
{
//include space character in given size
func include_space(_ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", terminator: "");
i += 1;
}
}
//include star symbol in given size
func include_star(_ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("* ", terminator: "");
i += 1;
}
}
//Print isosceles trapezoid star pattern of given size
func isosceles_trapezoid(_ size: Int)
{
print("\n Size : ", size ," \n\n", terminator: "");
//Loop controlling variable
var i: Int = 0;
//Loop, which is execute row operations
i = 0;
while (i < size)
{
self.include_space(size - i);
self.include_star(size + i);
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
obj.isosceles_trapezoid(4);
obj.isosceles_trapezoid(7);
obj.isosceles_trapezoid(10);
}
main();``````

#### Output

`````` Size :  4

* * * *
* * * * *
* * * * * *
* * * * * * *

Size :  7

* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *

Size :  10

* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *``````

## Explanation and Resultant Output

The code starts by defining three functions: `include_space`, `include_star`, and `isosceles_trapezoid`. The `include_space` function prints spaces based on the given size. The `include_star` function prints asterisks (*) based on the given size. The `isosceles_trapezoid` function prints the trapezoid pattern by combining the spaces and asterisks in a specific manner.

The `isosceles_trapezoid` function contains a loop that iterates from 0 to size-1. In each iteration, it calls the `include_space` function to print the appropriate number of spaces, and then it calls the `include_star` function to print the asterisks. By varying the number of spaces and asterisks, the trapezoid shape is formed. After printing each row, a new line character is printed to move to the next row.

The code provided in the example uses the `isosceles_trapezoid` function to print trapezoids of different sizes: 4, 7, and 10. The output is displayed below each pattern.

The time complexity of the code is O(n^2), where n is the size of the trapezoid. This is because there are two nested loops in the `isosceles_trapezoid` function, and the size of the loops is directly proportional to the input size. Therefore, as the size increases, the number of iterations increases quadratically.

## Finally

In this article, we have discussed how to print an isosceles trapezoid shape using a programming language. We provided an explanation of the problem, a suitable example, the algorithm and pseudocode, and an analysis of the time complexity. By understanding the logic behind the code, you can implement it in any programming language of your choice and customize it according to your needs.

## 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