Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Check username and password

C# Sharp String: Exercise-16 with Solution

Write a program in C# Sharp to check the username and password.

C# Sharp Exercises: Check username and password.

Sample Solution:-

C# Sharp Code:

using System;
public class Exercise16
{
    public static void Main()
    {
       string username, password;
       int ctr = 0;
       Console.Write("\n\nCheck username and password :\n");
	   Console.Write("N.B. : Default user name and password is :abcd and 1234\n");
       Console.Write("------------------------------------------------------\n"); 
         
        do
        {
			Console.Write("Input a username: ");
			username = Console.ReadLine();
 
			Console.Write("Input a password: ");
			password = Console.ReadLine();
			
             if(username != "abcd" || password != "1234")
             ctr++;
             else
             ctr=1;
     
        }
        while((username != "abcd" || password != "1234")  && (ctr != 3));
	 
        if (ctr == 3)
            Console.Write("\nLogin attemp three or more times. Try later!\n\n");
        else   
            Console.Write("\nThe password entered successfully!\n\n");		
    }
}

Sample Output:

Check username and password :                                                                                 
N.B. : Default username and password is : abcd and 1234                                                       
-------------------------------------------------------                                                       
Input a username: abcd                                                                                        
Input a password: 1234                                                                                        
                                                                                                              
The password entered successfully!

Flowchart:

Flowchart: Check username and password.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to read a sentence and replace lowercase characters by uppercase and vice-versa.
Next: Write a program in C# Sharp to search the position of a substring within a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.