How to write a id and password validation program in C

EX: Write a C program to print message only if id and password is correct. How to write a id and password validation program in C. C program for login using id and password.

In this program we will use Nested switch statement in C. In simple words Nested switch statement means switch statement defined inside the another switch statement. 


  Id and Password validation program in C:

#include<stdio.h>
void main(){

        //Declare variables
        int id;
        int pass;

        //Accept id from user
        printf("Enter Your id:\n ");
        scanf("%d",&id);

        switch(id) {
            //If id matches with case then accept password
            case 1010:
                printf("Enter your password:\n ");
                scanf("%d",&pass);

                switch(pass){
                    //If password matches with case then print message
                    case 1100:
                        printf("Welcome To codeforhunger.com\n");
                        break;
                    //print default message for password
                    default:
                        printf("Password is incorrect...");
                        break;
                }
                break;
                //print default message for id
            default:
                printf("Id is incorrect...");
                break;
        }   
}

Above program shows the following output:

Enter your id: 1010
Enter your password: 1100
Welcome to codeforhunger.com


  Same above program using if-else statements:


In above program we have used switch-case to validate id and password. But, we can also do this program using if-else statements. So let's see how it works....
 



#include<stdio.h>
void main(){

        //Declare variables
        int id;
        int pass;

        //Accept id from user
        printf("Enter Your id:\n ");
        scanf("%d",&id);

        if(id==1010) {
            //If given id matches with original id then accept password
                printf("Enter your password:\n ");
                scanf("%d",&pass);

                if(pass==1100){
                    //If given password matches with original password then print message
                        printf("Welcome To codeforhunger.com\n");
                }else{
                    //print default message for password
                        printf("Password is incorrect...");
                
                }
        }else{
                //print default message for id
                printf("Id is incorrect...");
        }   
}



Above Program shows the following output:

Enter your id: 1010
Enter your password: 1100
Welcome to codeforhunger.com

-If we enter incorrect input then it will show id or password is incorrect.

For example:
EX-1: 
Enter your id: 2300
Id is incorrect...
EX-2:
Enter your id: 1010
Enter your password: 5043
Password is incorrect...


Thank you for visiting us... If you have any doubts please feel free to discuss below in the comment section.


No comments:

Post a Comment

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