M N O and P 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 M, N, O, and P in Java.
1. Character Pattern M in Java
This program demonstrates the process of displaying the pattern M using Java programming language.
import java.util.*; class M { 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(j==0&&i>=0||i==j&&i<n/2||i+j==n-1&&i<n/2||j==n-1&&i>=0) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
Output of pattern M in java
Enter the value of n 8 * * ** ** * * * * * ** * * * * * * * * *
2. Character Pattern N in Java
This program demonstrates the process of printing the pattern N using Java programming language.
import java.util.*; class N { 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(j==0&&i>=0||i==j||j==n-1&&i>=0) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
Output of pattern N in java
Enter the value of n 8 * * ** * * * * * * * * * * * * * * ** * *
3. Character Pattern O in Java
This program demonstrates the how to display the pattern O using Java programming language.
import java.util.*; class O { 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(j==0&&i>=0||i==0&&j>=0||i==n-1&&j>=0||j==n-1&&i>=0) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
Output of pattern O in java
Enter the value of n 8 ******** * * * * * * * * * * * * ********
4. Character Pattern P in Java
This program demonstrates the how to print the pattern P using Java programming language.
import java.util.*; class P { 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(j==0&&i>=0||i==n/2&&j>=0||i==0&&j>=0||j==n-1&&i<=n/2) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
Output of pattern P in java
Enter the value of n 8 ******** * * * * * * ******** * * *
These series of articles deals with how to display character pattern programs in java, different diamond pattern programs in java, number pattern programs in java, printing pattern programs in java with explanation, string pattern programs in java, diamond number pattern programs in java, displaying 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. For regular updates please like the facebook page