Question #82892
Write a program that prompts for the name and password for three times of a user. The program displays "Congratulations, you are logged in." if the user enters the correct password or "Wrong password. Try Again." for a wrong password on which it displays "Sorry, your account is locked, contact the system administrator." if all the three attempts are wrong.
Use C programming language
Answer:
#include<stdio.h>
#include <string.h>
// define correct user password
const char password[256] = "abracadabra";
int main()
{
char user_name[256], user_password[256];
int attempts = 1; // number of attempts
int password_cmp = 1; // comparison result
// read the user name
printf("Enter user name: ");
scanf("%s", &user_name);
printf("Enter the password: ");
// loop password prompts
while (password_cmp != 0 && attempts <= 3) {
scanf("%s", &user_password); // read the password
// compare the correct password and user input
password_cmp = strcmp(password, user_password);
if (password_cmp != 0 && attempts < 3) {
printf("Wrong password. Try Again: ");
}
attempts++;
}
if (password_cmp == 0) { // correct password entered
printf("Congratulations, you are logged in.");
}
else { // wrong passwords entered
printf("Sorry, your account is locked, contact the system administrator.");
}
return 0;
}Screenshot