Java Program to Print Sandglass Alphabets Pattern

Write a Java program to print sandglass alphabets pattern using for loop.

package Alphabets;

import java.util.Scanner;

public class SandglassAlps1 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Sandglass Pattern of Alphabets Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Sandglass Alphabets Pattern\n");
		int i, j, k, alphabet = 64;
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = 1 ; j < i; j++ ) 
			{
				System.out.print(" ");
			}
			for(k = i; k <= rows ; k++) 
			{
				System.out.print((char)(alphabet + k) + " ");
			}
			System.out.println();
		}
		
		for (i = rows - 1; i >= 1; i-- ) 
		{
			for (j = 1 ; j < i; j++ ) 
			{
				System.out.print(" ");
			}
			for(k = i; k <= rows; k++) 
			{
				System.out.print((char)(alphabet + k) + " ");
			}
			System.out.println();
		}
	}
}
Java Program to Print Sandglass Alphabets Pattern

This Java program prints the sandglass pattern of alphabets using a while loop.

package Alphabets;

import java.util.Scanner;

public class SandglassAlps2 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Sandglass Pattern of Alphabets Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Sandglass Alphabets Pattern\n");
		int i, j, k, alphabet = 64;
		i = 1;
		
		while (i <= rows ) 
		{
			j = 1 ; 
			while(j < i ) 
			{
				System.out.print(" ");
				j++;
			}
			
			k = i;
			while( k <= rows ) 
			{
				System.out.print((char)(alphabet + k) + " ");
				k++;
			}
			System.out.println();
			i++;
		}
		
		i = rows - 1;
		while( i >= 1 ) 
		{
			j = 1 ; 
			while (j < i ) 
			{
				System.out.print(" ");
				j++;
			}
			
			k = i;
			while(k <= rows) 
			{
				System.out.print((char)(alphabet + k) + " ");
				k++;
			}
			System.out.println();
			i--;
		}
	}
}
Enter Sandglass Pattern of Alphabets Rows = 13
Printing Sandglass Alphabets Pattern

A B C D E F G H I J K L M 
 B C D E F G H I J K L M 
  C D E F G H I J K L M 
   D E F G H I J K L M 
    E F G H I J K L M 
     F G H I J K L M 
      G H I J K L M 
       H I J K L M 
        I J K L M 
         J K L M 
          K L M 
           L M 
            M 
           L M 
          K L M 
         J K L M 
        I J K L M 
       H I J K L M 
      G H I J K L M 
     F G H I J K L M 
    E F G H I J K L M 
   D E F G H I J K L M 
  C D E F G H I J K L M 
 B C D E F G H I J K L M 
A B C D E F G H I J K L M 

This Java example uses the do while loop to display the sandglass pattern of alphabets.

package Alphabets;

import java.util.Scanner;

public class SandglassAlps3 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Sandglass Pattern of Alphabets Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Sandglass Alphabets Pattern\n");
		int i, j, k, alphabet = 64;
		i = 1;
		
		do
		{
			j = 1 ; 
			do
			{
				System.out.print(" ");

			} while(j++ < i );
			
			k = i;
			do 
			{
				System.out.print((char)(alphabet + k) + " ");

			} while( ++k <= rows );
			System.out.println();

		} while (++i <= rows );
		
		i = rows - 1;
		do
		{
			j = 1 ; 
			do
			{
				System.out.print(" ");

			} while (j++ < i );
			
			k = i;
			do
			{
				System.out.print((char)(alphabet + k) + " ");

			} while(++k <= rows);
			System.out.println();

		} while( --i >= 1 ) ;
	}
}
Enter Sandglass Pattern of Alphabets Rows = 15
Printing Sandglass Alphabets Pattern

 A B C D E F G H I J K L M N O 
  B C D E F G H I J K L M N O 
   C D E F G H I J K L M N O 
    D E F G H I J K L M N O 
     E F G H I J K L M N O 
      F G H I J K L M N O 
       G H I J K L M N O 
        H I J K L M N O 
         I J K L M N O 
          J K L M N O 
           K L M N O 
            L M N O 
             M N O 
              N O 
               O 
              N O 
             M N O 
            L M N O 
           K L M N O 
          J K L M N O 
         I J K L M N O 
        H I J K L M N O 
       G H I J K L M N O 
      F G H I J K L M N O 
     E F G H I J K L M N O 
    D E F G H I J K L M N O 
   C D E F G H I J K L M N O 
  B C D E F G H I J K L M N O 
 A B C D E F G H I J K L M N O 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.