Program to Print Diamond with Repeated Character Pattern
In the previous article, we have discussed Java Program to Print Diamond Character Pattern
In this program we are going to see how to print the diamond character pattern.
- Java Code to Print Diamond with Repeated Character Pattern
- C Code to Print Diamond with Repeated Character Pattern
- C++ Code to Print Diamond with Repeated Character Pattern
Example-1 When row value=5 A B B B C C C C C B B B A
Example-2: When row value=9 A B B B C C C C C D D D D D D D E E E E E E E E E D D D D D D D C C C C C B B B A
Now, let’s see the actual program to print it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter total row and store it in an integer variable
row_count
. - Take two outer for loops, one for the upper half and the other for the bottom.
- Inside both the loops, take two inner for loops to print the space and the characters.
- After each iteration print a newline.
Java Code to Print Diamond with Repeated 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 and //then dividing it by two to get the size of the halves System.out.print("Rows(Enter odd number) : "); int row_count = scan.nextInt(); row_count/=2; //row, col are iterator and //the alphaAscii is the ASCII value //holder initialized to hold 'A' int row, col, alphaAscii=65; //loop to print lower part of the pattern for (row = 0; row <= row_count; row++) { //Inner loop to print space for (col = 2 * (row_count - row); col > 0; col--) { //printing space System.out.print(" "); } //Inner loop to print characters starting from 'A' for (col = 0; col <= 2 * row; col++) { //printing character System.out.print((char)(alphaAscii)+ " "); } alphaAscii++; System.out.println(); } alphaAscii -= 2; //loop to print lower part of the pattern for (row = 0; row <= row_count; row++) { //Inner loop to print space for (col = -1; col <= 2 * (row); col++) { //printing space System.out.print(" "); } for (col = 0; col < 2 * (row_count - row) - 1; col++) { //printing character System.out.print((char)(alphaAscii)+ " "); } alphaAscii--; System.out.println(); } } }
Output: Rows(Enter odd number) : 15 A B B B C C C C C D D D D D D D E E E E E E E E E F F F F F F F F F F F G G G G G G G G G G G G G H H H H H H H H H H H H H H H G G G G G G G G G G G G G F F F F F F F F F F F E E E E E E E E E D D D D D D D C C C C C B B B A
C Code to Print Diamond with Repeated Character Pattern
#include <stdio.h> int main() { printf("Rows(Enter odd number) : "); int row_count; scanf("%d", &row_count); row_count /= 2; //Taking number of rows as input from the user and //then dividing it by two to get the size of the halves int row, col, alphaAscii = 65; //row, col are iterator and the alphaAscii is the ASCII value holder for (row = 0; row <= row_count; row++) { //loop to print lower part of the pattern for (col = 2 * (row_count - row); col > 0; col--) { //Inner loop to print space printf(" "); } for (col = 0; col <= 2 * row; col++) { printf("%c ", (char)(alphaAscii)); } alphaAscii++; printf("\n"); } alphaAscii -= 2; for (row = 0; row <= row_count; row++) { //loop to print lower part of the pattern for (col = -1; col <= 2 * (row); col++) { //Inner loop to print space printf(" "); } for (col = 0; col < 2 * (row_count - row) - 1; col++) { printf("%c ", (char)(alphaAscii)); } alphaAscii--; printf("\n"); } return 0; }
Output: Rows(Enter odd number) : 15 A B B B C C C C C D D D D D D D E E E E E E E E E F F F F F F F F F F F G G G G G G G G G G G G G H H H H H H H H H H H H H H H G G G G G G G G G G G G G F F F F F F F F F F F E E E E E E E E E D D D D D D D C C C C C B B B A
C++ Code to Print Diamond with Repeated Character Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Rows(Enter odd number) : "; int row_count; cin >> row_count; row_count /= 2; //Taking number of rows as input from the user and //then dividing it by two to get the size of the halves int row, col, alphaAscii = 65; //row, col are iterator and the alphaAscii is the ASCII value holder for (row = 0; row <= row_count; row++) { //loop to print lower part of the pattern for (col = 2 * (row_count - row); col > 0; col--) { //Inner loop to print space cout << " "; } for (col = 0; col <= 2 * row; col++) { cout << (char)(alphaAscii) << " "; } alphaAscii++; cout << endl; } alphaAscii -= 2; for (row = 0; row <= row_count; row++) { //loop to print lower part of the pattern for (col = -1; col <= 2 * (row); col++) { //Inner loop to print space cout << " "; } for (col = 0; col < 2 * (row_count - row) - 1; col++) { cout << (char)(alphaAscii) << " "; } alphaAscii--; cout << endl; } return 0; }
Output: Rows(Enter odd number) : 15 A B B B C C C C C D D D D D D D E E E E E E E E E F F F F F F F F F F F G G G G G G G G G G G G G H H H H H H H H H H H H H H H G G G G G G G G G G G G G F F F F F F F F F F F E E E E E E E E E D D D D D D D C C C C C B B B A
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: