About Lesson
Write a program that simulates a traffic light. The user inputs the color (red, yellow, or green), and the program prints the corresponding message using a switch statement.
#include <stdio.h>
int main() {
char color;// Taking input for the traffic light color
printf(“Enter the traffic light color (red, yellow, or green): “);
scanf(“%s”, &color);// Using a switch statement to display the corresponding message
switch (color) {
case ‘r’:
case ‘R’:
printf(“Stop! The light is red.n”);
break;
case ‘y’:
case ‘Y’:
printf(“Caution! The light is yellow.n”);
break;
case ‘g’:
case ‘G’:
printf(“Go! The light is green.n”);
break;
default:
printf(“Invalid color. Please enter ‘red’, ‘yellow’, or ‘green’.n”);
}return 0;
}