Please solve the JAVA program.
Attachments:
![](https://hi-static.z-dn.net/files/d13/02c196351482a3c4c0d876ef1d814caf.jpg)
Answers
Answered by
10
/** JAVA PATTERN
*
* Copy the entire code into an IDE.
* The description and explanation are given as comments.
* Some screenshots are also attached.
*
* @author Purva
* @since 1st July 2018
*/
public class P25
{
public static void main(String[] args)
{
int spc; //Spaces before printing any number
int midspc; //Spaces between the two numbers in each row
int k=5; //The actual number to be printed
System.out.println();
for(int i=0;i<5;i++) //i prints 5 rows
{
for(spc=4;spc>i;spc--) //We need (4-i) spaces before printing any number
{
System.out.print(" ");
}
System.out.print(k); //Printing number for the first time
if(k==5) //The number 5 is to be printed only once. So loop is continued.
{
k--;
System.out.println();
continue;
}
for(midspc=1;midspc<(2*i);midspc++) //We need (2i-1) spaces after printing the first number
{
System.out.print(" ");
}
System.out.print(k); //Printing number second time in a row
k--; //Decrementing value of k by 1
System.out.println(); //Printing a new line for a new row
}
/* Now value of k is 0. And we have a pattern that looks like this:
*
* 5
* 4 4
* 3 3
* 2 2
* 1 1
*
* Now we print the decreasing sequence.
*
*/
k=2; //Resetting value of k to 2
for(int i=4;i>0;i--) //i will now print 4 rows. This time i is setting in decreasing order
{
for(spc=5;spc>i;spc--) //We need (5-i) spaces now. The decreasing order of i increases spaces in each row
{
System.out.print(" ");
}
System.out.print(k); //Printing the number for first time
if(k==5) //If number is 5, then loop is broken, and program ends
{
System.out.println();
break;
}
for(midspc=1;midspc<(2*(i-1));midspc++) //We need 2(i-1)-1 = 2i-3 spaces after printing number once
{
System.out.print(" ");
}
System.out.print(k); //Printing number for second time
k++; //Incrementing value of k by 1
System.out.println();
}
}
}
*
* Copy the entire code into an IDE.
* The description and explanation are given as comments.
* Some screenshots are also attached.
*
* @author Purva
* @since 1st July 2018
*/
public class P25
{
public static void main(String[] args)
{
int spc; //Spaces before printing any number
int midspc; //Spaces between the two numbers in each row
int k=5; //The actual number to be printed
System.out.println();
for(int i=0;i<5;i++) //i prints 5 rows
{
for(spc=4;spc>i;spc--) //We need (4-i) spaces before printing any number
{
System.out.print(" ");
}
System.out.print(k); //Printing number for the first time
if(k==5) //The number 5 is to be printed only once. So loop is continued.
{
k--;
System.out.println();
continue;
}
for(midspc=1;midspc<(2*i);midspc++) //We need (2i-1) spaces after printing the first number
{
System.out.print(" ");
}
System.out.print(k); //Printing number second time in a row
k--; //Decrementing value of k by 1
System.out.println(); //Printing a new line for a new row
}
/* Now value of k is 0. And we have a pattern that looks like this:
*
* 5
* 4 4
* 3 3
* 2 2
* 1 1
*
* Now we print the decreasing sequence.
*
*/
k=2; //Resetting value of k to 2
for(int i=4;i>0;i--) //i will now print 4 rows. This time i is setting in decreasing order
{
for(spc=5;spc>i;spc--) //We need (5-i) spaces now. The decreasing order of i increases spaces in each row
{
System.out.print(" ");
}
System.out.print(k); //Printing the number for first time
if(k==5) //If number is 5, then loop is broken, and program ends
{
System.out.println();
break;
}
for(midspc=1;midspc<(2*(i-1));midspc++) //We need 2(i-1)-1 = 2i-3 spaces after printing number once
{
System.out.print(" ");
}
System.out.print(k); //Printing number for second time
k++; //Incrementing value of k by 1
System.out.println();
}
}
}
Attachments:
![](https://hi-static.z-dn.net/files/d50/e4d5911b9787a03f2e583ce29adf6cd8.png)
![](https://hi-static.z-dn.net/files/dff/984f386f98c0bf1614b3d575ad919e92.png)
![](https://hi-static.z-dn.net/files/d6b/cea638f7bc05eb73615e6a50dc44a9f7.png)
![](https://hi-static.z-dn.net/files/d33/4123196f41cf0329d36e75b7bc46a5c0.png)
![](https://hi-static.z-dn.net/files/dd5/d4ddbe1d1863c5671dfccfca71d37360.jpg)
Similar questions