w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Test a password


C# Sharp Basic Data Types: Exercise-3 with Solution

Write a C# Sharp program that takes userid and password as input (type string). After 3 wrong attempts, user will be rejected.

Sample Solution:-

C# Sharp Code:

using System;
public class Exercise3
{
public static void Main()
{
string username, password;
int ctr = 0,dd=0;
       Console.Write("\n\nCheck username and password :\n");
	   Console.Write("N.B. : Defaule username and password is : username and password\n");
       Console.Write("---------------------------------\n"); 
do
{
Console.Write("Input a username: ");
username = Console.ReadLine();
 
Console.Write("Input a password: ");
password = Console.ReadLine();
if( username == "username" && password == "password" )
{
    dd=1;
    ctr=3;
}

else
{
    dd=0;
    ctr++;
}
}
while (( username != "username" || password != "password" )
        && (ctr != 3));
        if (dd == 0)
        {
            Console.Write("\nLogin attemp more than three times. Try later!\n\n");
        }
        else  
        if(dd==1)
        {
            Console.Write("\nPassword entered successfull!\n\n");	}         
    }
}

Sample Output:

Input a username: robin                                                                                       
Input a password: sds45                                                                                       
Input a username: john                                                                                        
Input a password: dfgrt                                                                                       
Input a username: doom                                                                                        
Input a password: jkkl                                                                                        
                                                                                                              
Login attemp more than three times. Try later!

Flowchart:

Flowchart: Test a password

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program that takes a number and a width also a number, as input and then displays a triangle of that width, using that number.
Next: Write a C# Sharp program that takes two numbers as input and perform an operation (+,-,*,x,/) on them and displays the result of that operation.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming