In java Use a nested for loop to create the following outpu
In java , Use a nested for loop to create the following output:
Using loops) Write a nested for loop that prints input: Using loops) Write a nested for loop that prints input:Solution
Please follow the code and comments for description :
a)
CODE :
public class XPatternPrinter { // class to run the code
public static void main(String[] args) { // driver method
System.out.println(\"Printing the Desired Pattern With the Numbers...\ \"); // prompt about the code
char[] charArray = {\'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\'}; // array of characters to print the pattern
int length = charArray.length; // get the length of the array
for (int i = 0; i < length; i++) { // iterate over the length
for (int j = 0; j < length; j++) { // inner length loop
if (j == i || j == (length - 1 - i)) { // check for the condition
System.out.print(charArray[j]); // print the data
} else {
System.out.print(\" \"); // else null value
}
}
System.out.println(); // new line character
}
}
}
OUTPUT :
Printing the Desired Pattern With the Numbers...
1 7
2 6
3 5
4
3 5
2 6
1 7
b)
CODE :
import java.util.Scanner; // required imports
public class UpsideDownTriangle { // class to run the code
public static void main(String args[]) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user
System.out.println(\"Printing the Desired Pattern...\ \"); // prompt about the code
int x, y, n; // local variables
System.out.println(\"Enter the Number of Rows Needed : \"); // prompt to enter the rows
n = sc.nextInt(); // get the data
for (y = n; y >= 1; y--) { //reverse here
for (x = 0; x < n - y; x++) {
System.out.print(\' \'); // print the empty character
}
for (x = (2 - y); x < (2 - y) + (2 * y - 1); x++) { // loop based on the condition calculated
System.out.print(\'*\'); // print the data
}
System.out.print(\'\ \'); // new line character
}
}
}
OUTPUT :
Printing the Desired Pattern...
Enter the Number of Rows Needed :
10
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Hope this is helpful.
Updated Codes and their Ouput:
a).
Class XPatternPrinter
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(\"X pattern numbers\");
for(int i=1;i<=4;i++){
for(int j=1;j<i;j++){
System.out.print(\" \");
}
System.out.print(\"\"+i);
for(int k=4-i;k>=0;k--){
System.out.print(\" \");
}
for(int l=4-i;l>1;l--){
System.out.print(\" \");
}
if(i!=4)
System.out.print(\"\"+(8-i));
System.out.println();
}
for(int i=1;i<4;i++){
for(int j=3-i-1;j>=0;j--){
System.out.print(\" \");
}
System.out.print(\"\"+i);
for(int l=1;l<=i;l++){
System.out.print(\" \");
}
for(int k=1;k<i;k++){
System.out.print(\" \");
}
System.out.print(\"\"+(8-i));
System.out.println();
}
}
}
OutPut:
b)
import java.lang.*;
class XPatternPrinter
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(\"Down Triangle Star Pattern\");
for(int i=1;i<=7;i++){
for(int j=0;j<i;j++){
System.out.print(\" \");
}
for(int k=7-i;k>=0;k--){
System.out.print(\"*\");
}
for(int l=7-i;l>0;l--){
System.out.print(\"*\");
}
System.out.println();
}
}
}
Output:


