Ex: write a C program to accept id from user and display department. How to write a C program to accept id from user and display department. C program to accept id from user and display department.
Input from user:
Enter your ID: 11
Expected output:
Software development
Quick links:
1. Accept input(id) from user declare variable say id.
2. Switch the value of id using switch(id) and match with cases.
3. Inside the body of switch we will give 6 possible values of no.
4. Therefore write 6 cases inside the switch.
5. If any case does not matches then for default case print invalid ID.
#include<stdio.h>
void main()
{
int id;
printf("Enter your ID: ");
scanf("%d",&id);
switch(id)
{
case 11:
case 12:
case 13:
printf("Software department");
break;
case 21:
case 22:
case 23:
printf("Developer department");
break;
default:
printf("Invalid ID");
}
}
Above program shows the following output:
Input from user:
Enter your ID: 11
Expected output:
Software development
Quick links:
Step by step logic of the given program:
1. Accept input(id) from user declare variable say id.
2. Switch the value of id using switch(id) and match with cases.
3. Inside the body of switch we will give 6 possible values of no.
4. Therefore write 6 cases inside the switch.
5. If any case does not matches then for default case print invalid ID.
C program to accept id from user to confirm department using switch-case:
#include<stdio.h>
void main()
{
int id;
printf("Enter your ID: ");
scanf("%d",&id);
switch(id)
{
case 11:
case 12:
case 13:
printf("Software department");
break;
case 21:
case 22:
case 23:
printf("Developer department");
break;
default:
printf("Invalid ID");
}
}
Above program shows the following output: