A B C and D character pattern programs in java
In this series of tutorial, we will study how to print character pattern programs in java. In this post, we will see how to create pattern A, B, C and D in Java.
1. Character Pattern A in Java
This program demonstrates how to display character pattern A using Java Programming language
import java.util.*; class A { public static void main(String[] args) { Scanner scan =new Scanner(System.in); System.out.println("Enter the value of n"); int n=scan.nextInt(); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if((i==0||i==n/2)&&(j>0&&j<n/2)||(j==0||j==n/2)&&i>0) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
The output of Pattern A in Java
Enter the value of n 8 *** * * * * * * ***** * * * * * *
2. Character Pattern B in Java
In this program we study how to display character pattern B using Java Programming language.
import java.util.*; class B { public static void main(String[] args) { Scanner scan =new Scanner(System.in); System.out.println("Enter the value of n"); int n=scan.nextInt(); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if((i==0||i==n/2||i==n-1)&&(j>0&&j<3*(n/4))||j==0|| (i>0&&i<n/2)&&j==3*(n/4)|| (i>n/2&&i<n-1)&&j==3*(n/4)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
The output of Pattern B in Java:
Enter the value of n 8 ****** * * * * * * ****** * * * * ******
3. Character Pattern C in Java
This program demonstrates how to display character pattern C using Java Programming language.
import java.util.*; class C { public static void main(String[] args) { Scanner scan =new Scanner(System.in); System.out.println("Enter the value of n"); int n=scan.nextInt(); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if((i==0||i==n-1)&&j!=0&&j<3*(n/4)||j==0&&(i>0&&i<n-1)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
The output of Pattern C in Java:
Enter the value of n 8 ***** * * * * * * *****
4. Character Pattern D in Java
In this program we study how to display character pattern D using Java Programming language.
import java.util.*; class D { public static void main(String[] args) { Scanner scan =new Scanner(System.in); System.out.println("Enter the value of n"); int n=scan.nextInt(); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if((i==0||i==n-1)&&j!=0&&j<3*(n/4)||j==0||j==3*(n/4)&&(i>0&&i<n-1)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
The output of Pattern D in Java:
Enter the value of n 8 ****** * * * * * * * * * * * * ******
These series of articles deals with how to print character pattern programs in java, different diamond pattern programs in java, number pattern programs in java, alphabet pattern programs in java, pattern programs in java with explanation, string pattern programs in java, diamond number pattern programs in java, square pattern programs in java. These are the commonly asked pattern programs in many campus and off campus placement interviews.
If you find anything incorrect please contact us at click here