128: Write a program that input your ATM password from the user and check if entered password is valid or not. If password is valid, display the messege: "Welcome to the Bank". Otherwise program ask to re-enter password 3 times If user enters the correct password, say: "Welcome to the bank". Otherwise display the messege: "Sorry your card is Captured".
With while loop:
// Write a program that input your ATM password from the user and check
// if entered password is valid or not.
// If password is valid, display the messege: "Welcome to the Bank".
// Otherwise program ask to re-enter password 3 times
// If user enters the correct password, say: "Welcome to the bank".
// Otherwise display the messege: "Sorry your card is Captured".
#include <iostream>
using namespace std;
int main()
{
int pin = 1234;
int password;
cout << "Enter your password: ";
cin >> password;
int i = 1;
while (password != pin && i < 3)
{
cout << "Invalid Input\n";
cout << "Re-enter your password: ";
cin >> password;
i++;
}
if (password == pin)
{
cout << "Welcome to the bank\n";
}
else if (password != pin)
{
cout << "Invalid Input\nSorry! Your card has been captured\n";
}
return 0;
}
With for loop:
// Write a program that input your ATM password from the user and check
// if entered password is valid or not.
// If password is valid, display the messege: "Welcome to the Bank".
// Otherwise program ask to re-enter password 3 times
// If user enters the correct password, say: "Welcome to the bank".
// Otherwise display the messege: "Sorry your card is Captured".
#include <iostream>
using namespace std;
int main()
{
int pin = 1234;
int password;
cout << "Enter your password: ";
cin >> password;
for (int i = 1; password != pin && i < 3; i++)
{
cout << "Invalid Input\n";
cout << "Re-enter your password: ";
cin >> password;
}
if (password == pin)
{
cout << "Welcome to the bank\n";
}
else if (password != pin)
{
cout << "Invalid Input\nSorry! Your card has been captured\n";
}
return 0;
}
With do-while loop:
// Write a program that input your ATM password from the user and check
// if entered password is valid or not.
// If password is valid, display the messege: "Welcome to the Bank".
// Otherwise program ask to re-enter password 3 times
// If user enters the correct password, say: "Welcome to the bank".
// Otherwise display the messege: "Sorry your card is Captured".
#include <iostream>
using namespace std;
int main()
{
int pin = 1234;
int password;
int i = 0;
do
{
if (i == 0)
{
cout << "Enter your password: ";
}
else
{
cout << "Invalid Input\n";
cout << "Re-enter your password: ";
}
cin >> password;
i++;
} while (password != pin && i < 3);
if (password == pin)
{
cout << "Welcome to the bank\n";
}
else if (password != pin)
{
cout << "Invalid input\nSorry! Your card has been captured\n";
}
return 0;
}
Comments
Post a Comment