Java Program to Generate N Number of Passwords of Length M Each

This is the java program to generate N passwords each of length M given by the user. The number of passwords, N, returned doesn’t exceed M!.

Here is the source code of the Java Program to Generate N Number of Passwords of Length M Each. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is sample program to generate N passwords of length M, where N < M!
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class N_Password_M_Length 
  6. {
  7.     static void permute(int []a, int k)
  8.     {
  9.         if(k==a.length)
  10.         {
  11.             for(int i=0; i<a.length; i++)
  12.             {
  13.                 System.out.print(a[i]);
  14.             }
  15.             System.out.println();	
  16.         }
  17.         else
  18.         {
  19.             for (int i = k; i<a.length; i++) 
  20.             {
  21.                 int temp=a[k];
  22.                 a[k]=a[i];
  23.                 a[i]=temp;
  24.  
  25.                 permute(a,k+1);
  26.  
  27.                 temp=a[k];
  28.                 a[k]=a[i];
  29.                 a[i]=temp;
  30.             }
  31.         }	
  32.  
  33.     }
  34.     public static void main(String args[])
  35.     {
  36.         System.out.println("Enter the length of the password: ");
  37.         Scanner input = new Scanner(System.in);
  38.         int m = input.nextInt();
  39.         Random random = new Random();
  40.         int []a = new int[m];
  41.         for(int i=0; i<m; i++)
  42.         {
  43.             a[i] = random.nextInt(10);
  44.         }
  45.  
  46.         System.out.println("The Possible Passwords are: ");
  47.         N_Password_M_Length.permute(a, 0);
  48.  
  49.         input.close();
  50.     }
  51. }

Output:

$ javac N_Password_M_Length.java
$ java N_Password_M_Length
 
Enter the length of the password: 
5
The Passwords are: 
31210
31201
31120
31102
31012
31021
32110
32101
32110
32101
32011
32011
31210
31201
31120
31102
31012
31021
30211
30211
30121
30112
30112
30121
13210
13201
13120
13102
13012
13021
12310
12301
12130
12103
12013
12031
11230
11203
11320
11302
11032
11023
10213
10231
10123
10132
10312
10321
21310
21301
21130
21103
21013
21031
23110
23101
23110
23101
23011
23011
21310
21301
21130
21103
21013
21031
20311
20311
20131
20113
20113
20131
11230
11203
11320
11302
11032
11023
12130
12103
12310
12301
12031
12013
13210
13201
13120
13102
13012
13021
10231
10213
10321
10312
10132
10123
01213
01231
01123
01132
01312
01321
02113
02131
02113
02131
02311
02311
01213
01231
01123
01132
01312
01321
03211
03211
03121
03112
03112
03121

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.