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

Loops Homework

This program allows a user to input a word and prints it in a pyramid formation. It uses nested for loops - an outer loop that iterates through each character of the input word, and an inner loop that prints each previous character plus the current one. It imports necessary classes like Scanner for input and String to access string methods. The main method gets user input, determines the word length, then uses the nested for loops to print each character plus all previous in successive rows to form a pyramid shape of the given word.

Uploaded by

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

Loops Homework

This program allows a user to input a word and prints it in a pyramid formation. It uses nested for loops - an outer loop that iterates through each character of the input word, and an inner loop that prints each previous character plus the current one. It imports necessary classes like Scanner for input and String to access string methods. The main method gets user input, determines the word length, then uses the nested for loops to print each character plus all previous in successive rows to form a pyramid shape of the given word.

Uploaded by

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

import java.util.

Scanner; //import scanner for user input


import java.lang.String; //the String class within java is required to access the
.charAt() method
public class WordPyramid {
public static void main(String args[]) //begin main method
{
Scanner input = new Scanner(System.in); //creates an instance of the
Scanner class called input
System.out.println (Enter a word: );//print the line
String word = input.nextLine();
int length = word.length();
for(int counter=0; counter<length; counter++) //sets up the first loop
which moves through
each letter in the string//
{
for(int index=0; index<counter; index++) //the next loop within the
first
{
System.out.printf("%c ",word.charAt(index));
}
System.out.printf("%c\n",word.charAt(counter));
}
}end main

}//end program

You might also like