Skip to content

Commit 459dbe2

Browse files
authored
Create RandomPasswordGenerator.cs
1 parent 0dadfdb commit 459dbe2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

RandomPasswordGenerator.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace RandomPasswordGenerator
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Console.WriteLine("Random Password Generator");
11+
Console.WriteLine("--------------------------");
12+
Console.Write("Enter the length of the password: ");
13+
int length = Convert.ToInt32(Console.ReadLine());
14+
15+
string password = GenerateRandomPassword(length);
16+
Console.WriteLine("Generated Password: " + password);
17+
Console.WriteLine("Press any key to exit...");
18+
Console.ReadKey();
19+
}
20+
21+
static string GenerateRandomPassword(int length)
22+
{
23+
const string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
24+
StringBuilder sb = new StringBuilder();
25+
Random rand = new Random();
26+
27+
for (int i = 0; i < length; i++)
28+
{
29+
int randomIndex = rand.Next(0, validChars.Length);
30+
sb.Append(validChars[randomIndex]);
31+
}
32+
33+
return sb.ToString();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)