Program to Print Trapezium Character Pattern
In the previous article, we have discussed Java Program to Print Double Sided Stair Case Character Pattern
In this article we are going to see how to print the trapezium character pattern.
- Java Code to Print Trapezium Character Pattern
- C Code to Print Trapezium Character Pattern
- C++ Code to Print Trapezium Character Pattern
Example-1: When characters in first line value=6 and lines value = 3 ABCDEF ABCDEFGHIJ ABCDEFGHIJKLMN
Now, let’s see the actual program to print it.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Enter total number of characters in first line and store it in an integer variable
size. - Then take total number of lines and store it in an integer variable
lines. - Take one outer for loop to iterate the lines.
- Take two inner for loops, one to print the space and the other one to print the character.
- After each iteration print a new line.
Java Code to Print Trapezium Character Pattern
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
// Starting ASCII value taken 65
int asciiAlpha = 65;
// Taking size as input from user
// enter even number (prefered)
System.out.print("characters 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
// enter even number (prefered)
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 characters
for(int j = 0; j < stars; j++){
System.out.print((char)(j + asciiAlpha));
}
//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: characters in first line : 2 lines : 4 AB ABCDEF ABCDEFGHIJ ABCDEFGHIJKLMN
C Code to Print Trapezium Character Pattern
#include <stdio.h>
int main()
{
printf("characters 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);
// Starting ASCII value taken 65
int asciiAlpha = 65;
//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("%c",(j + asciiAlpha));
}
//Prints a newline
printf("\n");
//decreases the number of spaces and
//increases the number of stars for each iteration
spaces--;
stars = stars + 4;
}
return 0;
}
Output: characters in first line : 2 lines : 4 AB ABCDEF ABCDEFGHIJ ABCDEFGHIJKLMN
C++ Code to Print Trapezium Character Pattern
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout << "characters 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;
// Starting ASCII value taken 65
int asciiAlpha = 65;
//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 << (char)(j + asciiAlpha);
}
//Prints a newline
cout << endl;
//decreases the number of spaces and
// increases the number of stars for each iteration
spaces--;
stars = stars + 4;
}
return 0;
}
Output: characters in first line : 2 lines : 4 AB ABCDEF ABCDEFGHIJ ABCDEFGHIJKLMN
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Character Pattern Programs: