Computer Science, asked by sunitakoley90, 1 year ago

Write a program in java forming a pattern:
12345
2345
345
45
5

Answers

Answered by Avengers00
34
\underline{\LARGE{\textbf{NUMBER PATTERN PRINTING PROGRAM IN JAVA:}}}

/* Java Program to print the number pattern */

import java.util.Scanner;

public class NumPattern

{  

   public static void printNumPattern(int n)  

       {  

           int i, j;  

           for(i=1; i<=n; i++)  

           {  

               for(j=i; j<=n; j++)  

                   {

                       System.out.print(j);  

                   }

               System.out.println();  

           }  

       }  

// Driver Function  

   public static void main(String args[])  

       {  

           Scanner scanner = new Scanner(System.in);

           System.out.print("Enter n: ");

           int n = scanner.nextInt();

           printNumPattern(n);  

       }  
}  

\underline{\Large{\textbf{Explanation :}}}

\begin{tabular}{l||c|c|c|c|c}Row-1&amp;1&amp;2&amp;3&amp;4&amp;5\\\cline{1-6}Row-2&amp;2&amp;3&amp;4&amp;5&amp;\\\cline{1-6}Row-3&amp;3&amp;4&amp;5&amp;&amp;\\\cline{1-6}Row-4&amp;4&amp;5&amp;&amp;&amp;\\\cline{1-6}Row-5&amp;5&amp;&amp;&amp;&amp;\end{tabular}

\sf\textsf{At first, the condition of pattern printing is to be found}\\ \sf\textsf{It is observed that there are 5 rows}

\sf\textsf{The numbers printed in each row}\\\sf\textsf{are the numbers starting from the row number to the n(which is taken as $\rm in put $ from the user) }

\sf\textsf{Here, in this case, n=5}

\sf\textsf{When n is printed in each row,}\\\sf\textsf{printing line has to be ended and change to next line,} \\\sf\textsf{which makes change of the row}

\sf\textsf{The same is continued for each row till the row number equals n}\\\sf\textsf{So 2 variables are required}\\ \sf\textsf{1. to track the row} \\\sf\textsf{2. to track the column}

\sf\textsf{Here, i is row control and j is the column control}\\\sf\textsf{First for $\rm lo op$ controls the numbers in each row}\\\sf\textsf{and second for controls number of columns.}

\sf\textsf{The value printing is done inside the second for $\rm lo op$}\\\sf\textsf{and values depends on the first for $\rm lo op$}\\\sf\textsf{(as numbers to be printed starts from the row number)}

\sf\textsf{so the condition of first for $\rm loo p$ is i$&lt;$=n ;}\\\sf\textsf{with i initialised to 1 and incrementing after each iteration}

\sf\textsf{and condition for second for $\rm lo op$ is j$&lt;$=i ;}\\\sf\textsf{with j initialised to i and incrementing after each iteration.}

\sf\textsf{Note that j initialiased to i to make the $\rm lo op$ print}\\\textsf{values starting from the row number(as given by i)}

\sf\textsf{System.out.println(); ends the line after the first for $\rm lo op$ evaluates to false}

\sf\textsf{Here, The n is taken from the user}

\sf\textsf{If there is no need of interaction between the user,}
\sf\textsf{Just the Driver Function is changed as follows}
// Driver Function  

   public static void main(String args[])  

       {  

           int n = 5;
     
           printNumPattern(n);  

       }  

\sf\textsf{And Change line-1 to import.java.io.*;}
Attachments:

muakanshakya: perfect ans ! :)
Avengers00: Thank you! ((:
Swarup1998: Great answer! :)
Avengers00: Thank you :)
Anonymous: Nice and.
Anonymous: Perfect ans.
Similar questions