samedi 17 juin 2017

Pattern printing output in correct format

Simple pattern programs that are asked in most of the campus interviews require coding online.

Can anyone give me correct solution to the problem of the output of pattern printing in Java?

I do not find any problem with the code but the output that is coming on my cmd is not ordered as the sample output.

Please help!

Here is the original code:

import java.util.Scanner;

public class MainClass
{
public static void main(String[] args) 
{
    Scanner sc = new Scanner(System.in);

    //Taking rows value from the user

    System.out.println("How many rows you want in this pattern?");

    int rows = sc.nextInt();

    System.out.println("Here is your pattern....!!!");

    for (int i = 1; i <= rows; i++) 
    {
        for (int j = 1; j <= i; j++)
        {
            System.out.print(j+" ");
        }

        System.out.println();
    }

    //Close the resources

    sc.close();
}

}

Here is sample output from a website:

          How many rows you want in this pattern?
           7
          Here is your pattern….!!!
            1
            1 2
            1 2 3
            1 2 3 4
            1 2 3 4 5
            1 2 3 4 5 6
            1 2 3 4 5 6 7

Here is my cmd output:

            How many rows you want in this pattern
            3
            Here is your pattern:
            1
            1
            2
            1
            2
            3

How to order the above output on my cmd? Every pattern program shows different output.

Aucun commentaire:

Enregistrer un commentaire