Given an integer N, the task is to generate random passwords of length N of easy, medium and strong level each.
Easy level Password: Consists of only numbers or letters. Medium level Password: Consists of Letters as well as numbers. Strong level Password - Consists of Letters, numbers, and/or special characters.
Examples:
Input: N = 5 Output: Easy level password (only numbers): 98990 Easy password (only letters): tpFEQ Medium level password: b3bC8 Strong level password: 7`74n Input: N = 7 Output: Easy level password (only numbers): 7508730 Easy level password (only letters): mDzKCjN Medium level password: 4Z05s66 Strong level password: 2384Qu9
Approach: Follow the steps below to solve the problem:
- For each password level, iterate through the given length.
- To generate each password, randomly assign characters and numbers using Random Number Generation based on the password level.
Below is the implementation of the above approach:
#include <iostream>
#include <cstdlib>
#include <ctime>
// Function to generate easy level passwords with only numbers
void easylevelpassnumbers(int n) {
std::cout << "Easy level password (only numbers): ";
for (int i = 0; i < n; i++) {
std::cout << rand() % 10; // Generate a random digit between 0 and 9
}
std::cout << "\n";
}
// Function to generate easy level passwords with only letters
void easylevelpassletters(int n) {
std::cout << "Easy level password (only letters): ";
for (int i = 0; i < n;) {
int d = rand() % 123; // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97) {
std::cout << static_cast<char>(d); // Convert the ASCII value to a character
i++;
}
}
std::cout << "\n";
}
// Function to generate medium level passwords with a mix of letters and numbers
void midlevelpass(int n) {
std::cout << "Medium level password: ";
for (int i = 0; i < n; i++) {
int d = rand() % 123; // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97) {
std::cout << static_cast<char>(d); // Convert the ASCII value to a character for letters
} else {
std::cout << d % 10; // Keep the last digit for numbers
}
}
std::cout << "\n";
}
// Function to generate strong level passwords with a mix of letters, numbers, and special characters
void stronglevelpass(int n) {
std::cout << "Strong level password: ";
for (int i = 0; i < n; i++) {
int d = rand() % 200; // Generate a random ASCII value between 0 and 199
if (d >= 33 && d <= 123) {
std::cout << static_cast<char>(d); // Convert the ASCII value to a character for valid range
} else {
std::cout << d % 10; // Keep the last digit for numbers outside the valid range
}
}
std::cout << "\n";
}
int main() {
int n = 5; // Length of the generated passwords
// Seed for random number generation using current time
std::srand(std::time(0));
// Generate passwords for different levels
easylevelpassnumbers(n);
easylevelpassletters(n);
midlevelpass(n);
stronglevelpass(n);
}
// C program to generate
// password of given length
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to generate easy level
// password with numbers
void easylevelpassnumbers(int n)
{
int i;
// Random character generation
// setting the seed as TIME
srand(time(NULL));
printf("Easy level password "
"(only numbers): ");
for (i = 0; i < n; i++) {
// rand() to assign random
// characters in the password
printf("%d", rand() % 10);
}
printf("\n");
}
// Function to generate easy level
// password with letters
void easylevelpassletters(int n)
{
int i, d;
printf("Easy level password"
" (only letters): ");
for (i = 0; i < n; i) {
d = rand() % 123;
if ((d >= 65 && d <= 90)
|| d >= 97) {
printf("%c", (char)d);
i++;
}
}
printf("\n");
}
// Function to generate random
// medium level password
void midlevelpass(int n)
{
int i, d;
printf("Medium level password: ");
for (i = 0; i < n; i++) {
d = rand() % 123;
// Random alphabetic characters
if ((d >= 65 && d <= 90)
|| d >= 97) {
printf("%c", (char)d);
}
else {
// Random digits
printf("%d", d % 10);
}
}
printf("\n");
}
// Function to generate strong
// level password
void stronglevelpass(int n)
{
int i, d;
printf("Strong level password: ");
for (i = 0; i < n; i++) {
d = rand() % 200;
// Random special characters
if (d >= 33 && d <= 123) {
printf("%c", (char)d);
}
else {
// Random digits
printf("%d", d % 10);
}
}
printf("\n");
}
// Driver Code
int main()
{
int n = 5;
easylevelpassnumbers(n);
easylevelpassletters(n);
midlevelpass(n);
stronglevelpass(n);
}
import java.util.Random;
public class PasswordGenerator {
// Function to generate easy level passwords with only numbers
static void easylevelpassnumbers(int n) {
System.out.print("Easy level password (only numbers): ");
Random rand = new Random();
for (int i = 0; i < n; i++) {
System.out.print(rand.nextInt(10)); // Generate a random digit between 0 and 9
}
System.out.println();
}
// Function to generate easy level passwords with only letters
static void easylevelpassletters(int n) {
System.out.print("Easy level password (only letters): ");
Random rand = new Random();
for (int i = 0; i < n;) {
int d = rand.nextInt(123); // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97) {
System.out.print((char) d); // Convert the ASCII value to a character
i++;
}
}
System.out.println();
}
// Function to generate medium level passwords with a mix of letters and numbers
static void midlevelpass(int n) {
System.out.print("Medium level password: ");
Random rand = new Random();
for (int i = 0; i < n; i++) {
int d = rand.nextInt(123); // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97) {
System.out.print((char) d); // Convert the ASCII value to a character for letters
} else {
System.out.print(d % 10); // Keep the last digit for numbers
}
}
System.out.println();
}
// Function to generate strong level passwords with a mix of letters, numbers, and special characters
static void stronglevelpass(int n) {
System.out.print("Strong level password: ");
Random rand = new Random();
for (int i = 0; i < n; i++) {
int d = rand.nextInt(200); // Generate a random ASCII value between 0 and 199
if (d >= 33 && d <= 123) {
System.out.print((char) d); // Convert the ASCII value to a character for valid range
} else {
System.out.print(d % 10); // Keep the last digit for numbers outside the valid range
}
}
System.out.println();
}
public static void main(String[] args) {
int n = 5; // Length of the generated passwords
// Seed for random number generation using current time
Random rand = new Random(System.currentTimeMillis());
// Generate passwords for different levels
easylevelpassnumbers(n);
easylevelpassletters(n);
midlevelpass(n);
stronglevelpass(n);
}
}
# Python program to generate
# password of given length
import random
import string
# Function to generate easy level password with numbers
def easy_level_pass_numbers(n):
# Random character generation
print("Easy level password (only numbers): ", end="")
for i in range(n):
# Random digits
print(random.randint(0, 9), end="")
print()
# Function to generate easy level password with letters
def easy_level_pass_letters(n):
# Random character generation
print("Easy level password (only letters): ", end="")
for i in range(n):
# Random alphabetic characters
print(random.choice(string.ascii_letters), end="")
print()
# Function to generate random medium level password
def mid_level_pass(n):
# Random character generation
print("Medium level password: ", end="")
for i in range(n):
# Random alphabetic characters or digits
print(random.choice(string.ascii_letters + string.digits), end="")
print()
# Function to generate strong level password
def strong_level_pass(n):
# Random character generation
print("Strong level password: ", end="")
for i in range(n):
# Random special characters or digits
print(random.choice(string.ascii_letters + string.digits + string.punctuation), end="")
print()
# Driver Code
if __name__ == '__main__':
n = 5
easy_level_pass_numbers(n)
easy_level_pass_letters(n)
mid_level_pass(n)
strong_level_pass(n)
# Contributed by adityasha4x71
using System;
class Program
{
// Function to generate easy level passwords with only numbers
static void EasyLevelPassNumbers(int n)
{
Console.Write("Easy level password (only numbers): ");
Random random = new Random();
for (int i = 0; i < n; i++)
{
Console.Write(random.Next(10)); // Generate a random digit between 0 and 9
}
Console.WriteLine();
}
// Function to generate easy level passwords with only letters
static void EasyLevelPassLetters(int n)
{
Console.Write("Easy level password (only letters): ");
Random random = new Random();
for (int i = 0; i < n;)
{
int d = random.Next(123); // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97)
{
Console.Write((char)d); // Convert the ASCII value to a character
i++;
}
}
Console.WriteLine();
}
// Function to generate medium level passwords with a mix of letters and numbers
static void MidLevelPass(int n)
{
Console.Write("Medium level password: ");
Random random = new Random();
for (int i = 0; i < n; i++)
{
int d = random.Next(123); // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97)
{
Console.Write((char)d); // Convert the ASCII value to a character for letters
}
else
{
Console.Write(d % 10); // Keep the last digit for numbers
}
}
Console.WriteLine();
}
// Function to generate strong level passwords with a mix of letters, numbers, and special characters
static void StrongLevelPass(int n)
{
Console.Write("Strong level password: ");
Random random = new Random();
for (int i = 0; i < n; i++)
{
int d = random.Next(200); // Generate a random ASCII value between 0 and 199
if (d >= 33 && d <= 123)
{
Console.Write((char)d); // Convert the ASCII value to a character for valid range
}
else
{
Console.Write(d % 10); // Keep the last digit for numbers outside the valid range
}
}
Console.WriteLine();
}
static void Main()
{
int n = 5; // Length of the generated passwords
// Generate passwords for different levels
EasyLevelPassNumbers(n);
EasyLevelPassLetters(n);
MidLevelPass(n);
StrongLevelPass(n);
}
}
// Function to generate easy level passwords with only numbers
function easylevelpassnumbers(n) {
console.log("Easy level password (only numbers): ");
for (let i = 0; i < n; i++) {
console.log(Math.floor(Math.random() * 10)); // Generate a random digit between 0 and 9
}
console.log("\n");
}
// Function to generate easy level passwords with only letters
function easylevelpassletters(n) {
console.log("Easy level password (only letters): ");
for (let i = 0; i < n;) {
let d = Math.floor(Math.random() * 123); // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97) {
console.log(String.fromCharCode(d)); // Convert the ASCII value to a character
i++;
}
}
console.log("\n");
}
// Function to generate medium level passwords with a mix of letters and numbers
function midlevelpass(n) {
console.log("Medium level password: ");
for (let i = 0; i < n; i++) {
let d = Math.floor(Math.random() * 123); // Generate a random ASCII value between 0 and 122
if ((d >= 65 && d <= 90) || d >= 97) {
console.log(String.fromCharCode(d)); // Convert the ASCII value to a character for letters
} else {
console.log(d % 10); // Keep the last digit for numbers
}
}
console.log("\n");
}
// Function to generate strong level passwords with a mix of letters, numbers, and special characters
function stronglevelpass(n) {
console.log("Strong level password: ");
for (let i = 0; i < n; i++) {
let d = Math.floor(Math.random() * 200); // Generate a random ASCII value between 0 and 199
if (d >= 33 && d <= 123) {
console.log(String.fromCharCode(d)); // Convert the ASCII value to a character for valid range
} else {
console.log(d % 10); // Keep the last digit for numbers outside the valid range
}
}
console.log("\n");
}
// Length of the generated passwords
let passwordLength = 5;
// Generate passwords for different levels
easylevelpassnumbers(passwordLength);
easylevelpassletters(passwordLength);
midlevelpass(passwordLength);
stronglevelpass(passwordLength);
//This code is contributed by adarsh
Output
Easy level password (only numbers): 11973 Easy level password (only letters): AZYnW Medium level password: oo7c6 Strong level password: ,5Cc4
Time Complexity: O(N)
Auxiliary Space: O(N)