May 27, 2021

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.


We write this program using Nested switch statement in C. In simple words Nested switch statement means switch statement defined inside the another switch statement. 


Program:

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

        //Declare variables
        int id= 1010;
        int pass = 1100;

        //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


Share This:    Facebook Twitter

0 comments:

Post a Comment

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