Password - C# Programming Exercise

In this C# programming exercise, the user is asked to enter their login and password, both as integer numbers. The goal is to create a program that repeatedly prompts the user for these inputs until the correct login, which must be "12", and the correct password, which must be "1234", are entered. This type of validation is crucial in many programs that require secure access through a login and password. By using a loop to keep asking for the data until the correct values are entered, the exercise also reinforces the importance of while loops and conditional structures such as if statements. Validating user input is a key part of creating applications that handle sensitive information, like authentication systems. This exercise is an excellent way to practice how to manage incorrect inputs and ensure data integrity in your program.

 Category

Flow Control

 Exercise

Password

 Objective

Write a C# program to prompt the user to enter their login and password (both must be integer numbers) and repeat the prompt as many times as necessary until the entered login is "12" and the password is "1234".

 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

        // Repeatedly prompt the user for login and password until they enter the correct ones
        do
        {
            // 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

            // If the login and password are not correct, the loop will repeat
            if (login != 12 || password != 1234)
            {
                Console.WriteLine("Invalid login or password. Please try again."); // Informing the user about invalid login/password
            }

        } while (login != 12 || password != 1234); // The loop continues until both login and password are correct

        // Displaying a success message once the correct login and password are entered
        Console.WriteLine("Login successful!"); // Informing the user that the login was successful
    }
}

 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#.

  •  Password V2

    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 ente...

  •  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...