Computer Applications

Write a program in Java to display the following pattern for n number of rows (take n as input from user):

Enter number of rows: 5
abcdedcba
abcdedc
abcde
abc
a

Java

Java Nested for Loops

14 Likes

Answer

import java.util.Scanner;

public class KboatPattern
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
        int n = in.nextInt();
        for (int i = n * 2 - 1; i >= 1; i -= 2) {
            char term = 96;
            for (int j = 1; j <= i; j++) {
                if (j > n)
                    term--;
                else
                    term++;
                System.out.print(term);
            }
            System.out.println();
        }
    }
}

Output

Answered By

6 Likes