- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Library Classes in Java
Chapter 12
Library Classes in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Word Triangle / Inverted Triangle
Define a class (using the Scanner class) to generate a pattern of a word in the form of a triangle or in the form of an inverted triangle, depending upon user's choice.
Sample Input:
Enter a word: CLASS
Enter your choice: 1
Sample Output:
C
CL
CLA
CLAS
CLASS
Enter your choice: 2
Sample Output:
CLASS
CLAS
CLA
CL
C
Java Program – Word Triangle / Inverted Triangle
import java.util.Scanner;
public class WordPattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = sc.next();
System.out.print("Enter your choice (1 for Triangle, 2 for Inverted Triangle): ");
int choice = sc.nextInt();
int length = word.length();
if(choice == 1)
{
// Triangle Pattern
for(int i = 1; i <= length; i++)
{
System.out.println(word.substring(0, i));
}
}
else if(choice == 2)
{
// Inverted Triangle Pattern
for(int i = length; i >= 1; i--)
{
System.out.println(word.substring(0, i));
}
}
else
{
System.out.println("Invalid Choice!");
}
sc.close();
}
}
Output
Sample Run 1: Input: Enter a word: CLASS Enter your choice: 1 Output : C CL CLA CLAS CLASS Sample Run 2: Input: Enter a word: CLASS Enter your choice: 2 Output : CLASS CLAS CLA CL C
