In this article, we are going to see how to print the trapezium star pattern in Java, C program, and C++ Program.
Example-1 When stars in first line value=4 and lines value = 5 **** ******** ************ **************** ********************
Example-2: When stars in first line value=6 and lines value = 5 ****** ********** **************
Now, let’s see the actual java program to print the trapezium pattern.
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Approach:
- Enter a total number of stars in the first line and store it in an integer variable size.
- Then take a total number of lines and store it in integer variable lines.
- Take one outer for loop to iterate the lines.
- Take two inners for loops, one to print the space and the other one to print the star.
- After each iteration print a new line.
Algorithm to Program to Print the Trapezium Pattern
- To read num which indicates the number of lines.
- We are diving the pattern into 2 halves that is LHS part and the RHS part.
- Ex: When num = 2
- LHS –
- 1*2*
- 3*
- RHS –
- 5*6
- 4
- Combining LHS and RHS we get the complete pattern.
JAVA Program to Print:
Method-1: Static Star Character – Java Program to Print Star Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // Taking size as input from user System.out.print("stars in first line : "); int size = scan.nextInt(); // number of stars in the first line // and spaces in our pattern int stars = size, spaces = size*2; //Taking number of lines // to print as input from the user System.out.print("Lines : "); int lines = scan.nextInt(); // Outer loop for specified number of lines for(int r = 0; r < lines; r++){ // Loop to print spaces for(int c = spaces; c > 1; c--){ System.out.print(" "); } // loop to print spaces for(int j = 0; j < stars; j++){ System.out.print("*"); } //Prints a newline System.out.println(); // deccreases the number of spaces and // increases the number of stars // for each iteration spaces--; stars = stars+4; } } }
Output:
stars in first line : 6 Lines : 3 ****** ********** **************
Method-2: User Input Character – Java Program to Print Trapezium Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // Taking size as input from user System.out.print("stars in first line : "); int size = scan.nextInt(); // number of stars in the first line // and spaces in our pattern int stars = size, spaces = size*2; //Taking number of lines // to print as input from the user System.out.print("Lines : "); int lines = scan.nextInt(); // Taking any random character // which will be used to print the trapezium System.out.print("Character : "); char tr = scan.next().charAt(0); // Outer loop for specified number of lines for(int r = 0; r < lines; r++){ // Loop to print spaces for(int c = spaces; c > 1; c--){ System.out.print(" "); } // loop to print spaces for(int j = 0; j < stars; j++){ System.out.print(tr); } //Prints a newline System.out.println(); // deccreases the number of spaces and // increases the number of stars // for each iteration spaces--; stars = stars+4; } } }
Output:
stars in first line : 6 Lines : 3 Character : @ @@@@@@ @@@@@@@@@@ @@@@@@@@@@@@@
C Code for printing Trapezium Star Pattern
#include <stdio.h> int main() { printf("stars in first line : "); //Taking size as input from user int size; scanf("%d", &size); //number of stars in the first line and spaces in our pattern int stars = size, spaces = size * 2; //Taking number of lines to print as input from the user printf("Lines : "); int lines; scanf("%d", &lines); //Outer loop for specified number of lines for (int r = 0; r < lines; r++) { //Loop to print spaces for (int c = spaces; c > 1; c--) { printf(" "); } //loop to print spaces for (int j = 0; j < stars; j++) { printf("*"); } //Prints a newline printf("\n"); //deccreases the number of spaces and increases the number of stars for each iteration spaces--; stars = stars + 4; } return 0; }
Output:
stars in first line : 6 Lines : 3 ****** ********** **************
C++ Code To Print Trapezium Star Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "stars in first line : "; //Taking size as input from user int size; cin >> size; //number of stars in the first line and spaces in our pattern int stars = size, spaces = size * 2; //Taking number of lines to print as input from the user cout << "Lines : "; int lines; cin >> lines; //Outer loop for specified number of lines for (int r = 0; r < lines; r++) { //Loop to print spaces for (int c = spaces; c > 1; c--) { cout << " "; } //loop to print spaces for (int j = 0; j < stars; j++) { cout << "*"; } //Prints a newline cout << endl; //deccreases the number of spaces and increases the number of stars for each iteration spaces--; stars = stars + 4; } return 0; }
Output stars in first line : 6 Lines : 3 ****** ********** **************
C# Program to Print Trapezium Pattern
// 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(); } } }
Related Java Star Pattern Programs: