KnowledgeBoat Logo

Java Pattern Programs

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

ICSE

12 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

BlueJ output of 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

Answered By

4 Likes