Computer Applications
Write a program (using scanner class) to generate a pattern of a token in the form of a triangle or in the form of an inverted triangle depending upon the user's choice.
Sample Input/Output:
Enter your choice 1
*
* *
* * *
* * * *
* * * * *
Enter your choice 2
* * * * *
* * * *
* * *
* *
*
Java
Input in Java
19 Likes
Answer
import java.util.Scanner;
public class KboatPattern
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Triangle");
System.out.println("2. Inverted Triangle");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice) {
case 1:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
break;
case 2:
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
break;
default:
System.out.println("Invalid Choice");
break;
}
}
}
Variable Description Table
Program Explanation
Output


Answered By
9 Likes
Related Questions
Using scanner class, write a program to input temperatures recorded in different cities in °F (Fahrenheit). Convert and print each temperature in °C (Celsius). The program terminates when user enters a non-numeric character.
Write a program to accept a set of 50 integers. Find and print the greatest and the smallest numbers by using scanner class method.
In a competitive examination, a set of 'N' number of questions results in 'True' or 'False'. Write a program by using scanner class to accept the answers. Print the frequency of 'True' and 'False'.
Write a program to accept a sentence in mixed case. Find the frequency of vowels in each word and print the words along with their frequencies in separate lines.
Sample Input:
We are learning scanner classSample Output:
Word Frequency of vowels
We 1
are 2
learning 3
scanner 2
class 1