Java Program to Generate N Number of Passwords of Length M Each
Last Updated :
07 Oct, 2022
Java program to generate N number of passwords each of length M. The number of passwords returned doesn’t exceed the length M.
Example:
Input : N = 1, M = 3
Output: 571
Input : N = 2, M = 4
Output: 5671
1987
Approach:
- Import random package for creating random numbers.
- Initialize variables N and M.
- Create an array of N length.
- Run nested loops
- First loop for generating N numbers of passwords.
- Second loop for creating the password of M length.
Below is the implementation of the above approach:
Java
import java.util.Random;
import java.util.Scanner;
public class GFG {
public static void main(String args[])
{
Random r = new Random();
int N = 5 ;
int M = 8 ;
int [] a = new int [N];
for ( int j = 0 ; j < N; j++) {
for ( int i = 0 ; i < M; i++) {
System.out.print(a[j] = r.nextInt( 10 ));
}
System.out.println();
}
}
}
|
Output
73807243
05081188
63921767
70426689
06272980
Time Complexity: O(N*M), where N is the number of passwords required and M is the length of each password.
Auxiliary Space: O(N) as using extra space for array a of size N
Share your thoughts in the comments
Please Login to comment...