Given ‘num’ which indicates number of lines.The task is to print a trapezium pattern in num lines.
Examples:
Input : 4
Output :
1*2*3*4*17*18*19*20
5*6*7*14*15*16
8*9*12*13
10*11
Input : 2
Output :
1*2*5*6
3*4
Algorithm :
step 1.To read num which indicates number of lines.
step 2.We are diving pattern into 2 halves that is LHS part and RHS part.
Ex : When num = 2
LHS –
1*2*
3*
RHS –
5*6
4
step 3.Combining LHS and RHS we get the complete pattern.
C++
// CPP program to print Trapezium Pattern #include <iostream> using namespace std; int main() { int num = 3; int space; int i, j, lterm, rterm; // The terms on the LHS of the pattern lterm = 1; // The terms on the RHS of the pattern rterm = num * num + 1; for (i = num; i > 0; i--) { // To print number of spaces for (space = num; space > i; space--) cout << " "; for (j = 1; j <= i; j++) { cout << lterm; cout << "*"; lterm++; } for (j = 1; j <= i; j++) { cout << rterm; if (j < i) printf("*"); rterm++; } // To get the next term on RHS of the Pattern rterm = rterm - (i - 1) * 2 - 1; cout << endl; } } |
chevron_right
filter_none
Java
// Java program to print Trapezium Pattern import java.io.*; import java.util.*; public class HelloWorld { public static void main(String[] args) { int num = 3; int space; int i, j, lterm, rterm; lterm = 1; // The terms on the LHS of the pattern // The terms on the RHS of the pattern rterm = num * num + 1; for (i = num; i > 0; i--) { // To print number of spaces for (space = num; space > i; space--) System.out.print(" "); for (j = 1; j <= i; j++) { System.out.print(lterm); System.out.print("*"); lterm++; } for (j = 1; j <= i; j++) { System.out.print(rterm); if (j < i) System.out.print("*"); rterm++; } // To get the next term on RHS of the Pattern rterm = rterm - (i - 1) * 2 - 1; System.out.println(); } } } |
chevron_right
filter_none
Python 3
# Python 3 program to print # Trapezium Pattern if __name__ == "__main__": num = 3 # The terms on the LHS # of the pattern lterm = 1 # The terms on the RHS # of the pattern rterm = num * num + 1 for i in range(num,-1,-1): # To print number of spaces for space in range(num,i-1,-1): print(" ",end="") for j in range(1, i+1): print(str(lterm)+"*",end="") lterm += 1 for j in range(1,i+1): print(rterm,end="") if j < i: print("*",end="") rterm += 1 # To get the next term on RHS of the Pattern rterm = rterm - (i - 1) * 2 - 1 print() # This code is contributed by ChitraNayal |
chevron_right
filter_none
C#
// C# program to print Trapezium Pattern using System; public class HelloWorld { public static void Main(String[] args) { // Scanner scn = new Scanner(System.in); int num = 3; int space; // System.out.println("Enter number of lines : "); // num = scn.nextInt(); int i, j, lterm, rterm; lterm = 1; // The terms on the LHS of the pattern // The terms on the RHS of the pattern rterm = num * num + 1; for (i = num; i > 0; i--) { // To print number of spaces for (space = num; space > i; space--) Console.Write(" "); for (j = 1; j <= i; j++) { Console.Write(lterm); Console.Write("*"); lterm++; } for (j = 1; j <= i; j++) { Console.Write(rterm); if (j < i) Console.Write("*"); rterm++; } // To get the next term on RHS of the Pattern rterm = rterm - (i - 1) * 2 - 1; Console.WriteLine(); } } } // This code is contributed by ankita_saini |
chevron_right
filter_none
PHP
<?php // PHP program to print // Trapezium Pattern $num = 3; $space; $i; $j; $lterm; $rterm; // The terms on the LHS // of the pattern $lterm = 1; // The terms on the // RHS of the pattern $rterm = $num * $num + 1; for ($i = $num; $i > 0; $i--) { // To print number of spaces for ($space = $num; $space > $i; $space--) echo " "; for ($j = 1; $j <= $i; $j++) { echo $lterm; echo "*"; $lterm++; } for ($j = 1; $j <= $i; $j++) { echo $rterm; if ($j < $i) echo "*"; $rterm++; } // To get the next term // on RHS of the Pattern $rterm = $rterm - ($i - 1) * 2 - 1; echo "\n"; } // This code is contributed // by Akanksha Rai(Abby_akku) ?> |
chevron_right
filter_none
Output:
Enter number of lines : 3
1*2*3*10*11*12
4*5*8*9
6*7
Recommended Posts:
- Program to calculate area and perimeter of Trapezium
- Program to print the pattern ‘D’
- Program to print the pattern 'G'
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to print pattern
- Program to print Spiral Pattern
- Program to print pyramid pattern
- Program to print circle pattern
- Program to print V and inverted-V pattern
- Program to print the arrow pattern
- Program to print Crown Pattern
- Program to print Hut Star pattern
- Program to print numeric pattern | Set - 2
- Program to print a rectangle pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



