Mapping Floor Numbers to Names using a Switch Statement in C


This program is a simple program that takes an input of a floor number and prints the corresponding floor name using a switch statement.

  • The program starts by including the stdio.h header file, which contains the declarations of the functions used in the program.
  • In the main function, the program declares an integer variable "a" to store the floor number entered by the user.
  • It uses the printf function to prompt the user to enter the floor number and uses the scanf function to read the input and store it in the "a" variable.
  • The program then uses a switch statement to check the value of "a" and match it with the cases in the switch statement. Each case has a corresponding floor name.
  • In the case where the floor number entered by the user matches the case, the corresponding floor name is printed using the printf function.
  • In the default case, if the floor number entered by the user does not match any of the cases, the program will print "Invalid No"
  • Finally, it returns 0 to indicate that the program has executed successfully.

Source Code

#include <stdio.h>
int main()
{
int a;
printf("Enter Floor No : ");
scanf("%d",&a);
switch(a)
{
case 1:
  printf("\nFirst Floor");
break;
case 2:
  printf("\nSecond Floor");
break;
case 3:
  printf("\nThird Floor");
break;
default:
  printf("Invalid No");
break;
}
return 0;
}
To download raw file Click Here

Output

Output:
Enter Floor No:3
Third Floor

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C