samedi 12 mars 2022

Print an upside down triangle recursively

I am trying to figure out how to print an upside-down triangle recursively, using only one for-loop. I keep getting an error. I'm able to print the first row but I'm having a hard time recalling the function to print the remaining rows and decrementing n.

    public static void printTriangle (int n) {
        if( n == 0 ) 
            System.out.println("");
        for (int i = n; i >0; i--) {
            System.out.print("*");    
        }
        System.out.println();
        printTriangle(n-1);
    }

Aucun commentaire:

Enregistrer un commentaire