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

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_