Password As String - C# Programming Exercise

This exercise in C# aims to develop a program that asks the user for their username and password (both as strings) and repeats the prompt as many times as necessary until the entered username is exactly "username" and the password is exactly "password". The program uses loops, such as a while loop, to validate the entered credentials and ensure that the values are correct before proceeding.

This exercise is ideal for practicing the use of control structures such as loops and conditionals in C#. It also reinforces skills in string comparison and handling user input. It is a fundamental activity for understanding how to implement basic authentication systems and credential validation in C# programming. This type of exercise is very common in applications requiring user interaction and data verification.

 Category

Basic Data Types

 Exercise

Password As String

 Objective

Write a C# program to ask the user for their username and password (both should be strings) and repeat it as many times as necessary until the entered name is "username" and the password is "password".

 Write Your C# Exercise

using System;  // Import the System namespace to use basic classes like Console

class Program  // Define the main class of the program
{
    static void Main()  // The entry point of the program
    {
        string username;  // Declare a variable to store the username
        string password;  // Declare a variable to store the password

        // Loop until the correct username and password are entered
        do
        {
            // Ask the user for their username
            Console.Write("Enter your username: ");
            username = Console.ReadLine();  // Read the username input from the user

            // Ask the user for their password
            Console.Write("Enter your password: ");
            password = Console.ReadLine();  // Read the password input from the user

            // Check if the username and password are correct
            if (username != "username" || password != "password")  // If either is incorrect
            {
                Console.WriteLine("Invalid username or password, please try again.");  // Display error message
            }

        } while (username != "username" || password != "password");  // Repeat until both are correct

        // When correct username and password are entered
        Console.WriteLine("Welcome! You have successfully logged in.");  // Welcome message
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

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 5 attempts

    This C# exercise involves creating a program that prompts the user for their username and password, both as strings. If the entered credentials do not m...

  •  Calculator - if

    This C# programming exercise requires creating a program that asks the user for two numbers and an operation to perform on them. The supported operations are ...

  •  Calculator - switch

    In this C# programming exercise, you need to create a program that asks the user for two numbers and a mathematical operation to perform between them. Supporte...

  •  Double

    In this C# programming exercise, you need to create a program that calculates the perimeter, area, and diagonal of a rectangle, given its widt...

  •  Calculate values of a function

    In this C# programming exercise, you need to create a program that calculates and displays certain values of the function y = x² - 2x + 1, using integer numbers for ...

  •  Display a function

    In this C# programming exercise, you must create a program to "draw" the graph of the function y = (x - 4)² for integer values of x ranging from -1 to 8. The result will be visuall...