Alphabet M shape Pattern in java
M shape Pattern
Disclaimer:-
to understand what happen in this pattern i will recommend you to watch my blog that " How to make V shape Pattern"
to watch this click here
Steps to do this:-
1. take 3 integers i,j,n ( i and j is for loop and n is for user input length)
2. take a length from user
3.now take 2 integers p1 and p2 and initialize them
p1=1;
p2=n*2-1;
4. start the i loop (i=1;i<=n;i++) and inside i loop start the j loop
(j=1;j<=n*2;j++) and check
if(j==p1|| j==p2 || j==1 || j==n*2)
{
print "*";
}
else
{
print " ";
}
now close the j loop and do
p1++;
p2--;
print a new line inside i loop
Explanation:-
- when we check if (j==p1) it will print left to bottom line
*
*
*
*
- when we check if (j==p2) it will print right to bottom line
*
*
*
*
- when we check if j==1 it will print the 1st column
*
*
*
*
- when we check if j==n*2 it will print the last column
*
*
*
*
*
and at last it will print our M shape pattern5. Save and run
The Source Code
import java.util.Scanner;
class mpattern
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int i,j,n;
System.out.println("enter the length");
n=s.nextInt();
int p1=1;
int p2=n*2-1;
for(i=1;i<=n;i++)
{
for (j=1; j<=n*2;j++ )
{
if (j==p1||j==p2||j==1||j==n*2)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
p1++;
p2--;
System.out.println();
}
}
}
OUTPUT
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know