Computer Science, asked by Anonymous, 1 year ago

Please solve the JAVA program.

Attachments:

Answers

Answered by QGP
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();
        }
    }
}
Attachments:
Similar questions