Ex: write a C program to check password is correct or incorrect using switch-case. How to write a C program to check given password is correct or incorrect using switch-case. C program to check password is correct or incorrect using switch-case.
Input from user:
Enter your password: 1010
Expected output:
Welcome
Quick links:
1. Accept input (password) from user declare variable say ps.
2. Switch the value of ps using switch(ps).
3. There is 1 possible value of ps(1010).
4. Therefore write 1 case inside the switch.
5. If case does not match then for default case print incorrect password.
#include<stdio.h>
int main()
{
int ps;
printf("Enter your password: ");
scanf("%d",&ps);
switch(ps)
{
case 1010:
printf("--WELCOME--");
break;
default:
printf("-INCORRECT PASSWORD-");
}
return 0;
}
Above program shows the following output:
Input from user:
Enter your password: 1010
Expected output:
Welcome
Quick links:
Step by step logic of the given program:
1. Accept input (password) from user declare variable say ps.
2. Switch the value of ps using switch(ps).
3. There is 1 possible value of ps(1010).
4. Therefore write 1 case inside the switch.
5. If case does not match then for default case print incorrect password.
C program to check password is correct or incorrect using switch-case :
#include<stdio.h>
int main()
{
int ps;
printf("Enter your password: ");
scanf("%d",&ps);
switch(ps)
{
case 1010:
printf("--WELCOME--");
break;
default:
printf("-INCORRECT PASSWORD-");
}
return 0;
}
Above program shows the following output:
this is not executing i think the syntax is wrong
ReplyDeleteIt's working fine... Run this code on online compiler
DeleteThank you for visiting us :)