Program to Print Pant Style Character Pattern
In the previous article, we have discussed Java Program to Print letters of a string in Right Angled Triangle Character Pattern
In this program we are going to see how to print inverted pant style character pattern.
- Java Code to Print Inverted Pant Style Character Pattern
- C Code to Print Inverted Pant Style Character Pattern
- C++ Code to Print Inverted Pant Style Character Pattern
Example-1 When row value=5 A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
Example-2: When row value=9 A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEF FEDCBA ABCDEFG GFEDCBA ABCDEFGH HGFEDCBA ABCDEFGHI IHGFEDCBA ABCDEFGHIJIHGFEDCBA
Now, let’s see the actual program to print it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach:
- Enter total row and store it in an integer variable
row_count
. - Take one outer for loop to iterate the rows,
- Inside the for loop, take three inner for loops, one to print the space and the other for characters.
- After each iteration print a new line.
Java Code to Print Inverted Pant Style Character Pattern
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Taking total rows as input from the user System.out.print("Rows : "); int row_count = scan.nextInt(); int row,col; // Ascii value if 'A' int asciiAlpha = 65; // Outer Loop for (row = row_count; row >= 0; row--) { // Inner loop to print character for(col=0;col<=row_count-row;col++) { System.out.print((char)(asciiAlpha+col)); } // Inner loop to print space for(col=1;col<=row*2-1;col++) { System.out.print(" "); } // Inner loop to print character for(col = row_count-row;col>=0; col--) { if(col!=row_count) System.out.print((char)(asciiAlpha+col)); } // Prints a newline System.out.println(); } } }
Output Rows : 5 A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
C Code to Print Inverted Pant Style Character Pattern
#include <stdio.h> #include <string.h> int main() { //Taking total rows as input from the user printf("Rows : "); int row_count; scanf("%d", &row_count); int row, col; // Ascii value if 'A' int asciiAlpha = 65; // Outer Loop for (row = row_count; row >= 0; row--) { // Inner loop to print character for (col = 0; col <= row_count - row; col++) { printf("%c", (asciiAlpha + col)); } // Inner loop to print space for (col = 1; col <= row * 2 - 1; col++) { printf(" "); } // Inner loop to print character for (col = row_count - row; col >= 0; col--) { if (col != row_count) printf("%c", (asciiAlpha + col)); } // Prints a newline printf("\n"); } return 0; }
Output Rows : 5 A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
C++ Code to Print Inverted Pant Style Character Pattern
#include <iostream> #include <cstring> #include <string.h> using namespace std; int main(int argc, char const *argv[]) { //Taking total rows as input from the user cout << "Rows : "; int row_count; cin >> row_count; int row, col; // Ascii value if 'A' int asciiAlpha = 65; // Outer Loop for (row = row_count; row >= 0; row--) { // Inner loop to print character for (col = 0; col <= row_count - row; col++) { cout << (char)(asciiAlpha + col); } // Inner loop to print space for (col = 1; col <= row * 2 - 1; col++) { cout << " "; } // Inner loop to print character for (col = row_count - row; col >= 0; col--) { if (col != row_count) cout << (char)(asciiAlpha + col); } // Prints a newline cout << endl; } return 0; }
Output: Rows : 5 A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
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: