0% found this document useful (0 votes)
14 views

JAVA7FOR10

Uploaded by

singh2gamers
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

JAVA7FOR10

Uploaded by

singh2gamers
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Write a Java program to take a string as input from the user and print it in a pyramid shape

import java.util.Scanner;
public class PyramidPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
int length = inputString.length();
for (int i = 0; i < length; i++) {
for (int spaces = 0; spaces < length - i - 1; spaces++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(inputString.charAt(j) + " ");
}
System.out.println();
}
} }
OUTPUT:
Enter a string: Hello
H
He
Hel
Hell
Hello

Variable Description Chart:

Variable Name Data Type Use


inputString String Stores the input string from the user
length int Stores the length of the input string
spaces int Stores the number of spaces to print before the string
i, j int Loop variables

You might also like