Program to Print Pyramid with Column wise Same Character Pattern
In the previous article, we have discussed Java Program to Print Pyramid with Column wise Increasing Character Pattern
In this program we are going to see how to print the pyramid column wise same character pattern.
- Java Code to Print Pyramid with Column wise Same Character Pattern
- C Code to Print Pyramid with Column wise Same Character Pattern
- C++ Code to Print Pyramid with Column wise Same Character Pattern
Example-1 When row value=5 A BAB CBABC DCBABCD EDCBABCDE
Example-2: When row value=9 A BAB CBABC DCBABCD EDCBABCDE FEDCBABCDEF GFEDCBABCDEFG HGFEDCBABCDEFGH IHGFEDCBABCDEFGHI
Now, let’s see the actual program to print it.
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
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 Pyramid with Column wise Same Character Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Taking number of rows as input from the user System.out.print("Rows : "); int row_count = scan.nextInt(); int row, col; //Outer for loop to iterate the rows for (row = 1; row <= row_count; row++) { //Loop to print space for (col = 1;col <=row_count-row;col++) { System.out.print(" "); } //Loop to print char for (col = row; col > 0; col--) { System.out.print((char)(col+64)); } //Loop to print char for (col = 2; col <= row; col++) { System.out.print((char)(col+64)); } //Prints a newline System.out.println(); } } }
Output: Rows : 9 A BAB CBABC DCBABCD EDCBABCDE FEDCBABCDEF GFEDCBABCDEFG HGFEDCBABCDEFGH IHGFEDCBABCDEFGHI
C Code to Print Pyramid with Column wise Same Character Pattern
#include <stdio.h> int main() { //Taking number of rows as input from the user printf("Rows : "); int row_count; scanf("%d", &row_count); int row, col; // Outer for loop to iterate the rows for (row = 1; row <= row_count; row++) { //Loop to print space for (col = 1; col <= row_count - row; col++) { printf(" "); } //Loop to print char for (col = row; col > 0; col--) { printf("%c", (col + 64)); } //Loop to print char for (col = 2; col <= row; col++) { printf("%c", (col + 64)); } //prints a newline printf("\n"); } return 0; }
Output: Rows : 9 A BAB CBABC DCBABCD EDCBABCDE FEDCBABCDEF GFEDCBABCDEFG HGFEDCBABCDEFGH IHGFEDCBABCDEFGHI
C++ Code to Print Pyramid with Column wise Same Character Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { //Taking number of rows as input from the user cout << "Rows : "; int row_count; cin >> row_count; int row, col; // Outer for loop to iterate the rows for (row = 1; row <= row_count; row++) { //Loop to print space for (col = 1; col <= row_count - row; col++) { cout << " "; } //Loop to print char for (col = row; col > 0; col--) { cout << (char)(col + 64); } //Loop to print char for (col = 2; col <= row; col++) { cout << (char)(col + 64); } // prints a newline cout << endl; } return 0; }
Output: Rows : 9 A BAB CBABC DCBABCD EDCBABCDE FEDCBABCDEF GFEDCBABCDEFG HGFEDCBABCDEFGH IHGFEDCBABCDEFGHI
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: