Program to Print Hollow Diamond within Rectangle Character Pattern
In the previous article, we have discussed Java Program to Print Right Pascal Character Pattern
In this program we are going to see how to print the hollow diamond within rectangle character pattern.
- Java Code to Print Hollow Diamond within Rectangle Character Pattern
- C Code to Print Hollow Diamond within Rectangle Character Pattern
- C++ Code to Print Hollow Diamond within Rectangle Character Pattern
Example-1 When row value=5 ABCBA AB BA A A AB BA ABCBA
Example-2: When row value=11 ABCDEFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
Now, let’s see the actual program to print it.
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
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 three inner for loops, one to print the space and the other two for characters.
- After each iteration print a newline.
Java Code to Print Hollow Diamond within Rectangle 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(enter odd number) : "); int row_count = scan.nextInt(); row_count=row_count/2; //Making the row count half to print each half of the pattern int row, col; int asciiValue = 65; //ASCII of A is 65 for (row = 0; row <= row_count; row++) { //Outer loop to print upper half for (col = 0; col <= row_count-row; col++) { //Loop to print character System.out.print((char)(asciiValue + col)); } for (col = 1; col <= row*2-1; col++) { //Loop to print space System.out.print(" "); } for (col = row_count-row; col >= 0; col--) { //Loop to print character if(col!=row_count) System.out.print((char)(asciiValue + col)); } System.out.println(); //Prints a newline } for (row = row_count - 1; row >= 0; row--) { //Inner loop to print lower half for (col = 0; col <= row_count-row; col++) { //Loop to print character System.out.print((char)(asciiValue + col)); } for (col = 1; col <= row*2-1; col++) { //Loop to print space System.out.print(" "); } for (col = row_count-row; col >= 0; col--) { //Loop to print character if(col!=row_count) System.out.print((char)(asciiValue + col)); } System.out.println(); //Prints a new line } } }
Output: Rows(enter odd number) : 11 ABCDEFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
C Code to Print Hollow Diamond within Rectangle Character Pattern
#include <stdio.h> int main() { //Taking number of rows as input from the user printf("Rows(enter odd number) : "); int row_count; scanf("%d", &row_count); row_count = (row_count) / 2; //Making the row count half to print each half of the pattern int row, col; int asciiValue = 65; //ASCII value of A is 65 for (row = 0; row <= row_count; row++) { //Outer loop to print upper half for (col = 0; col <= row_count - row; col++) { //Loop to print character printf("%c", (asciiValue + col)); } for (col = 1; col <= row * 2 - 1; col++) { //Loop to print space printf(" "); } for (col = row_count - row; col >= 0; col--) { //Loop to print character if (col != row_count) printf("%c", (asciiValue + col)); } printf("\n"); //Prints a newline } for (row = row_count - 1; row >= 0; row--) { //Inner loop to print lower half for (col = 0; col <= row_count - row; col++) { //Loop to print character printf("%c", (asciiValue + col)); } for (col = 1; col <= row * 2 - 1; col++) { //Loop to print space printf(" "); } for (col = row_count - row; col >= 0; col--) { //Loop to print character if (col != row_count) printf("%c", (asciiValue + col)); } printf("\n"); //Prints a newline } return 0; }
Output Rows(enter odd number) : 11 ABCDEFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A AB BA ABC CBA ABCD DCBA ABCDE EDCBA ABCDEFEDCBA
C++ Code to Print Hollow Diamond within Rectangle 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(enter odd number) : "; int row_count; cin >> row_count; row_count /= 2; int row, col; //Converting the character to its ascii value int asciiValue = 65; //ASCII value of A is 65 for (row = 0; row <= row_count; row++) { //Outer loop to print upper half for (col = 0; col <= row_count - row; col++) { //Loop to print character cout << (char)(asciiValue + col); } for (col = 1; col <= row * 2 - 1; col++) { //Loop to print space cout << " "; } for (col = row_count - row; col >= 0; col--) { //Loop to print character if (col != row_count) cout << (char)(asciiValue + col); } cout << endl; //Prints a newline } for (row = row_count - 1; row >= 0; row--) { //Inner loop to print lower half for (col = 0; col <= row_count - row; col++) { //Loop to print character cout << (char)(asciiValue + col); } for (col = 1; col <= row * 2 - 1; col++) { //Loop to print space cout << " "; } for (col = row_count - row; col >= 0; col--) { //Loop to print character if (col != row_count) cout << (char)(asciiValue + col); } cout << endl; //Prints a newline } return 0; }
Output: Rows(enter odd number) : 11 ABCDEFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA 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:
- Java Program to Print Pyramid with Column wise Increasing Character Pattern
- Java Program to Print Pyramid with Column wise Same Character Pattern
- Java Program to Print letters of a string in Inverted Right Angled Triangle Character Pattern
- Java Program to Print letters of a string in Right Angled Triangle Character Pattern