Ask Question
18 March, 10:09

Write a java program that will print out the following pattern 1 12 123 1234 12345

+2
Answers (1)
  1. 18 March, 10:47
    0
    public class Main {

    public static void main (String[] args)

    {

    int n, m;

    int k=5;

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

    {

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

    System. out. print (m);

    System. out. print (" ");

    }

    }

    }

    Explanation:

    The solution to this problem is the use of nested loops of an inner and outer loop to generate a half pyramid of numbers, but displayed on the same line so we have a sequence 1 12 123 1234 12345. The outer loop iterates for n=1 to n<=5 and the inner loop that has the print statement prints the integers from for m = 1 to m<=n.

    Note that the value k = 5 is hard coded which in some sense is the number of rows, that is the length of iteration of the outer loop.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a java program that will print out the following pattern 1 12 123 1234 12345 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers