Program to Print Sand Glass Character Pattern
In the previous article, we have discussed Java Program to Print Reverse K Shape Character Pattern
In this program we are going to see how to print the sand glass character pattern.
- Java Code to Print Sand Glass Character Pattern
- C Code to Print Sand Glass Character Pattern
- C++ Code to Print Sand Glass Character Pattern
Example-1 When row value=6 A B C B C C C B C A B C
Example-2: When row value=10 A B C D E B C D E C D E D E E E D E C D E B C D E A B C D E
Now, let’s see the actual program to print it.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
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 new line.
Java Code to Print Sand Glass Character Pattern
import java.util.Scanner; class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Rows(Enter even number) : "); int row_count = scan.nextInt(); 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; //row, col are iterator and the alphaAscii is the ASCII value holder initialized to hold 'A' for (row = 0; row <= row_count - 1; row++) { //loop to print upper part of the pattern alphaAscii = 65; //Resetting the value to 'A' after every iteration for (col = 0; col < row; col++) { //Inner loop to print space System.out.print(" "); } for (col = row; col <= row_count-1; col++) { //Inner loop to print character System.out.print((char)(alphaAscii+col)+" "); } System.out.println(); } for (row = row_count - 1; row >= 0; row--) { //loop to print lower part of the pattern alphaAscii = 65; //Resetting the value to 'A' after every iteration for (col = 0; col < row; col++) { //Inner loop to print space System.out.print(" "); } for (col = row; col <= row_count-1; col++) { //Inner loop to print character System.out.print((char)(alphaAscii+col)+" "); } System.out.println(); } } }
Output: Rows(Enter even number) : 10 A B C D E B C D E C D E D E E E D E C D E B C D E A B C D E
C Code to Print Sand Glass 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; //row, col are iterator and the alphaAscii is the ASCII value holder for (row = 0; row <= row_count - 1; row++) { //loop to print upper part of the pattern alphaAscii = 65; //Resetting the value to 'A' after every iteration for (col = 0; col < row; col++) { //Inner loop to print space printf(" "); } for (col = row; col <= row_count - 1; col++) { //Inner loop to print character printf("%c ", (char)(alphaAscii + col)); } printf("\n"); } for (row = row_count - 1; row >= 0; row--) { //loop to print lower part of the pattern alphaAscii = 65; //Resetting the value to 'A' after every iteration for (col = 0; col < row; col++) { //Inner loop to print space printf(" "); } for (col = row; col <= row_count - 1; col++) { //Inner loop to print character printf("%c ", (char)(alphaAscii + col)); } printf("\n"); } return 0; }
Output: Rows(Enter even number) : 10 A B C D E B C D E C D E D E E E D E C D E B C D E A B C D E
C++ Code to Print Sand Glass Character Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Rows(Enter even 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 - 1; row++) { //loop to print upper part of the pattern alphaAscii = 65; //Resetting the value to 'A' after every iteration for (col = 0; col < row; col++) { //Inner loop to print space cout << " "; } for (col = row; col <= row_count - 1; col++) { //Inner loop to print character cout << (char)(alphaAscii + col)<<” “; } cout << endl; } for (row = row_count - 1; row >= 0; row--) { //loop to print lower part of the pattern alphaAscii = 65; //Resetting the value to 'A' after every iteration for (col = 0; col < row; col++) { //Inner loop to print space cout << " "; } for (col = row; col <= row_count - 1; col++) { //Inner loop to print character cout << (char)(alphaAscii + col)<<” “; } cout << endl; } return 0; }
Output: Rows(Enter even number) : 10 A B C D E B C D E C D E D E E E D E C D E B C D E A B C D E
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: