C Exercises

Jun 19, 2020

C program to check password is correct or incorrect using switch-case

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:





  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:


C program to check password is correct or incorrect using switch-case

2 comments:

  1. this is not executing i think the syntax is wrong

    ReplyDelete
    Replies
    1. It's working fine... Run this code on online compiler
      Thank you for visiting us :)

      Delete

If you have any doubts, please discuss here...