Skip to main content

Display Trapezium Pattern

Here given code implementation process.

//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++) {

    //Add space
    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++) {
        //Add space

        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++) {

      //Add space
      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++) {
			//Add space

			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++) {
			//Add space

			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




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.

New Comment