Find Present Elevator Position using Switch Case in C++


This is a C++ program that prompts the user to enter a floor number and then uses a switch statement to print the corresponding floor name. Here's a breakdown of the code:

  • int a; This line declares a variable a of type int to store the floor number.
  • cout<<"Enter Floor No : "; cin>>a; These lines prompt the user to enter a floor number and read the input into the a variable using the cin function.
  • switch(a) { ... } This is a switch statement that takes the value of the a variable and executes the corresponding case. If a is 1, it will execute the code block after case 1. If a is 2, it will execute the code block after case 2, and so on. If a doesn't match any of the cases, it will execute the code block after the default case.
  • cout<<"\nFirst Floor"; These are examples of the code block that is executed when the a variable matches the corresponding case. For example, when a is 1, it will output "First Floor" using the cout function.
  • break; This keyword is used to exit the switch statement and continue executing the program after the switch statement.
  • cout<<"Invalid No"; This line is the code block that is executed when the a variable doesn't match any of the cases. It outputs "Invalid No" using the cout function.
  • return 0; This line ends the program and returns 0 to the operating system to indicate that the program executed successfully.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Enter Floor No : ";
    cin>>a;
    switch(a)
    {
        case 1:
            cout<<"\nFirst Floor";
            break;
        case 2:
            cout<<"\nSecond Floor";
            break;
        case 3:
            cout<<"\nThird Floor";
            break;
        default:
            cout<<"Invalid No";
            break;
    }
    return 0;
}
To download raw file Click Here

Output

Enter Floor No : 1
First Floor


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples