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
C# Exercise Example
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
}
}
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#.
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...
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 ...
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...
In this C# programming exercise, you need to create a program that calculates the perimeter, area, and diagonal of a rectangle, given its widt...
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 ...
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...