Password V2 - C# Programming Exercise

This C# exercise aims to create a program that asks the user for their login and password (both must be integer numbers) and repeats the request until the entered login is "12" and the password is "1234". However, the user will have a maximum of three attempts to enter the correct credentials. This is useful to ensure that users don't try to guess the login and password indefinitely. This exercise reinforces the use of a while loop or do-while loop in combination with validation to limit the user's attempts and prevent potential unauthorized access. Additionally, the program uses an if statement structure to check if the entered data is correct and a counter to track the attempts. Implementing these features makes the program suitable for security situations where access should be restricted after several failed attempts. This exercise helps understand how to handle incorrect inputs and establish security restrictions efficiently.

 Category

Flow Control

 Exercise

Password V2

 Objective

Write a C# program to ask the user for their login and password (both must be integer numbers) until the entered login is "12" and the password is "1234". The user will have a maximum of three attempts.

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        int login; // Declaring a variable to store the login
        int password; // Declaring a variable to store the password
        int attempts = 0; // Variable to count the number of attempts

        // Repeatedly prompt the user for login and password until they enter the correct ones or exceed 3 attempts
        while (attempts < 3) // Loop will repeat until attempts are less than 3
        {
            // Asking the user to enter their login (login should be an integer)
            Console.Write("Enter your login: ");
            login = int.Parse(Console.ReadLine()); // Reading the login entered by the user

            // Asking the user to enter their password (password should be an integer)
            Console.Write("Enter your password: ");
            password = int.Parse(Console.ReadLine()); // Reading the password entered by the user

            // Check if the entered login and password are correct
            if (login == 12 && password == 1234)
            {
                // Display a success message and exit the loop
                Console.WriteLine("Login successful!"); // Informing the user that the login was successful
                return; // Exiting the program once the correct login and password are entered
            }
            else
            {
                // Incrementing the attempt counter if the login or password is incorrect
                attempts++; // Increase the number of attempts made
                Console.WriteLine($"Invalid login or password. You have {3 - attempts} attempts left."); // Informing the user about remaining attempts
            }
        }

        // If the user exceeds the maximum number of attempts, display a failure message
        Console.WriteLine("You have exceeded the maximum number of attempts. Access denied."); // Informing the user that they've failed
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Many divisions

    This C# exercise aims to develop a program that asks the user for two numbers and displays the result of the division of these numbers along with the remainder of the divisi...

  •  Several multiplication tables, (use do while)

    This C# exercise aims to create a program that displays multiplication tables from 2 to 6 using nested do...while loops. A do...while loop executes a block of ...

  •  Square

    This C# exercise aims to create a program that prompts the user to enter a number and a width, then displays a square with the given width, using...

  •  Break & continue

    This C# exercise aims to develop a program that displays the even numbers from 10 to 20, both inclusive, except for 16, using three different approaches...

  •  Rectangle V2

    This C# exercise aims to develop a program that asks the user for a number, a width, and a height, and then displays a rectangle with the provide...

  •  Repetitive structures

    This C# exercise aims to develop a program that prompts the user for two numbers and displays all the numbers between them (inclusive) three times using for, while...