Alphabet T pattern in java
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. start the i loop from (i=1;i<=n;i++) start the j loop inside i loop and from (j=1;j<=n;j++) now check
if (i==1 || j==n/2)
{
print "*";
}
else
{
print " ";
}
close the j loop and print a new line inside i loop
Explanation:-
when we check if(i==1) it will print the 1st row
************
when we check if(j==n/2) it will print the middle column
*
*
*
*
*
and at the end of the loop it will print T
4.Save and run
import java.util.Scanner;
class tpattern {
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();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i == 1 || j == (n / 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
} OUTPUT
2. take a length from user
3. start the i loop from (i=1;i<=n;i++) start the j loop inside i loop and from (j=1;j<=n;j++) now check
if (i==1 || j==n/2)
{
print "*";
}
else
{
print " ";
}
close the j loop and print a new line inside i loop
Explanation:-
when we check if(i==1) it will print the 1st row
************
when we check if(j==n/2) it will print the middle column
*
*
*
*
*
and at the end of the loop it will print T
4.Save and run
The Source code
import java.util.Scanner;
class tpattern {
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();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i == 1 || j == (n / 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
} OUTPUT
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know