Write a Java Program to Print Alphabet A using stars with an example. In this Java Alphabet A pattern example, we used nested for loop and if statement to print the output.
package ShapePrograms; import java.util.Scanner; public class AlphabetA { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter the Number = "); int rows = sc.nextInt(); System.out.println("---- Printing Alphabet A ------"); for (int i = 0 ; i < rows; i++ ) { for (int j = 0 ; j <= rows/2; j++ ) { if((i == 0 && j != 0 && j != rows/2) || i == rows / 2 || (j == 0 || j == rows /2 ) && i != 0) System.out.print("*"); else System.out.print(" "); } System.out.println("\n"); } } }
In this Java Alphabet A using stars pattern example, we replaced the for loop with a while loop.
package ShapePrograms; import java.util.Scanner; public class AlphabetA2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter the Number = "); int rows = sc.nextInt(); System.out.println("---- Printing Alphabet A ------"); int j, i = 0; while ( i < rows ) { j = 0 ; while ( j <= rows/2) { if((i == 0 && j != 0 && j != rows/2) || i == rows / 2 || (j == 0 || j == rows /2 ) && i != 0) System.out.print("*"); else System.out.print(" "); j++; } System.out.println("\n"); i++; } } }
Please Enter the Number = 7
---- Printing Alphabet A ------
**
* *
* *
****
* *
* *
* *
Java Program to Print Alphabet A using stars using do while loop.
package ShapePrograms; import java.util.Scanner; public class AlphabetA3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter the Number = "); int rows = sc.nextInt(); System.out.println("---- Printing Alphabet A ------"); int j, i = 0; do { j = 0 ; do { if((i == 0 && j != 0 && j != rows/2) || i == rows / 2 || (j == 0 || j == rows /2 ) && i != 0) System.out.print("*"); else System.out.print(" "); } while ( ++j <= rows/2); System.out.println("\n"); } while ( ++i < rows ) ; } }
Please Enter the Number = 10
---- Printing Alphabet A ------
****
* *
* *
* *
* *
******
* *
* *
* *
* *