Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that displays a pyramid pattern. Use a nested for the loop that

ID: 3624053 • Letter: W

Question

Write a program that displays a pyramid pattern. Use a nested for the loop that prints the following output: You need to figure out how many spaces to print before the numbers. This depends on the number. If a single digit, print four spaces. If a number has two digits, print three spaces,. If a number has three digits, print two spaces. The Math.pow() method was introduced in dollar 2.12.1, "Problem: Computing Loan Payments". Before submitting the program, you should test run it , and make sure it works properly. Submit your source code (the file Folder)

Explanation / Answer

please rate - thanks

import java.util.*;
public class pascal
{public static void main(String[] args)
{int row, col,num,j,spaces;
for(row=0;row<=7;row++)
    {for(col=1; col<=7-row;col++)
         System.out.print("     ");
     for(col=0; col<=row;col++)
            {num=(int)Math.pow(2,col);
               if(num<10)
                    spaces=4;
                else if(num<100)
                    spaces=3;
                else
                    spaces=2;
                for(j=0;j<spaces;j++)
                    System.out.print(" ");
                System.out.print(num);
            }
    for(col=row-1;col>=0;col--)
           {num=(int)Math.pow(2,col);
               if(num<10)
                    spaces=4;
                else if(num<100)
                    spaces=3;
                else
                    spaces=2;
                for(j=0;j<spaces;j++)
                    System.out.print(" ");
                System.out.print(num);
            }
     System.out.print(' ');
    }


}
}
        

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote