PLUS SIGN IN JAVA LANGUAGE
AMRUHA AHMED
14th December,2023.
This article will aid you in the creation of a plus sign in Java using a combination of nested loops.
DELVING DEEPER:
- 1.This pattern requires the usage of nested loops to print the asterisks in the appropriate manner
- 2.A separate variable needs to be reserved to keep track of the number of spaces printed before the asterisks are printed on the screen
BREAK-UP OF THE PATTERN:
- 1.A total of 11 rows need to be printed in the pattern
- 2.The dashes shown above are an indication of spaces required. A separate variable for space is initialized to 5 which remains constant for each iteration.
- 3.In case the middle of the pattern is not reached (row 6 is not reached) then, after the spaces are printed on the screen , then a single asterisk is now printed. This step ensures the creation of the vertical line of the plus sign.
- 4.In case the middle of the pattern( that is row 6) is reached then no space is printed. Instead, asterisks are printed in the entire row. This helps us to create the horizontal line of the plus sign.
PROGRAM:
import java.util.*;
class pluspattern
{
void main()
{
int m=5;//spaces before the *
int i,j;//loop counters
for(i=1;i<=11;i++)
{
if(i!=6)//when the iteration hasn't reached the middle of the pattern
{
for(j=1;j<=m;j++)
System.out.print(" ");
System.out.print("* ");
}
else//outer loop has reached the middle of the pattern
{
for(j=1;j<=11;j++)//printing the horizontal line
System.out.print("* ");
}
System.out.println();
}
}
}
OUTPUT: