Pattern questions using java

 public class Main
{
    public static void main(String[] args) {
        for (int i=1;i<=5; i++){
            for (int j=1; j<=i; j++){
                System.out.print(j+" ");
            }
            System.out.printf("\n");
        }
    }
}

 Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
 


public class Main
{
    public static void main(String[] args) {
        for (int i=5;i>=1; i--){
            for (int j=1; j<=i; j++){
                System.out.print(j+" ");
            }
            System.out.printf("\n");
        }
    }
}

Output

1 2 3 4 5
1 2 3 4
1 2 3
1 2


public class Main
{
    public static void main(String[] args) {
        for (int i=1;i<=5; i++){
            for (int j=i; j>=1; j--){
                System.out.print(j+" ");
            }
            System.out.printf("\n");
        }
    }
}

Output

1 2 3 4 5
1 2 3 4
1 2 3
1 2

 

public class Main
{
    public static void main(String[] args) {
        for (int i=5;i>=1; i--){
            for (int j=i; j>=1; j--){
                System.out.print(j+" ");
            }
            System.out.printf("\n");
        }
    }
}

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1




Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER