Using the switch statement in Java, write a program to display the colour of the spectrum
(VIBGYOR) according to the user's choice. The user is expected to enter any of the alphabet of the
word VIBGYOR and the program output must display the respective colour starting with the alphabet
entered by the user.
import java.util.Scanner;
public class SpectrumColor {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Ask user for input
System.out.print("Enter a character from VIBGYOR: ");
char choice = sc.next().toUpperCase().charAt(0); // Convert to uppercase for uniformity
// Use switch statement to determine the color
switch (choice) {
case 'V':
System.out.println("V - Violet");
break;
case 'I':
System.out.println("I - Indigo");
break;
case 'B':
System.out.println("B - Blue");
break;
case 'G':
System.out.println("G - Green");
break;
case 'Y':
System.out.println("Y - Yellow");
break;
case 'O':
System.out.println("O - Orange");
break;
case 'R':
System.out.println("R - Red");
break;
default:
System.out.println("Invalid input! Please enter a letter from VIBGYOR.");
sc.close();