samedi 22 février 2020

Pattern print in java

import java.util.Scanner;

public class numPattern1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int i = 1;
        while (i <= n) {
            int space = n - i + 1;
            while (space <= n) {
                System.out.print(" ");
                space++;
            }
            int j = 1;
            int p = i;
            while (j <= i) {
                System.out.print(p);
                p++;
                j++;
            }
            System.out.println();
            i++;
        }
    }
}

output :

1
  23
   345
    4567

but i want pattern in opposite direction:

       1
     23
   345
 4567

Aucun commentaire:

Enregistrer un commentaire