KnowledgeBoat Logo

Computer Applications

Write a program to accept a word (say, BLUEJ) and display the pattern:

B L U E J
B L U E
B L U
B L
B

Java

Java String Handling

ICSE

72 Likes

Answer

import java.util.Scanner;

public class KboatStringPattern
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String word = in.nextLine();
        int len = word.length();

        for (int i = len - 1; i >= 0; i--) {
            for (int j = 0; j <= i; j++) {
                char ch = word.charAt(j);
                System.out.print(ch);
            }
            System.out.println();
        }
    }
}

Output

BlueJ output of Write a program to accept a word (say, BLUEJ) and display the pattern: B L U E J B L U E B L U B L B

Answered By

23 Likes


Related Questions