C++ Program to Generate N Number of Passwords of Length M Each

This is a C++ Program to generate N passwords each of length M. This problem focuses on finding the N permutations each of length M.

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

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. void permute(int *a, int k, int size)
  8. {
  9.     if (k == size)
  10.     {
  11.         for (int i = 0; i < size; i++)
  12.         {
  13.             cout << *(a + i);
  14.         }
  15.         cout << endl;
  16.     }
  17.     else
  18.     {
  19.         for (int i = k; i < size; i++)
  20.         {
  21.             int temp = a[k];
  22.             a[k] = a[i];
  23.             a[i] = temp;
  24.  
  25.             permute(a, k + 1, size);
  26.  
  27.             temp = a[k];
  28.             a[k] = a[i];
  29.             a[i] = temp;
  30.         }
  31.     }
  32.  
  33. }
  34. int main(int argc, char **argv)
  35. {
  36.     cout << "Enter the length of the password: ";
  37.     int m;
  38.     cin >> m;
  39.     int a[m];
  40.     for (int i = 0; i < m; i++)
  41.     {
  42.         /*generates random number between 1 and 10*/
  43.         a[i] = rand() % 10;
  44.     }
  45.     for (int i = 0; i < m; i++)
  46.     {
  47.         cout << a[i] << ", ";
  48.     }
  49.     cout << "The Passwords are: ";
  50.     permute(a, 0, m);
  51. }

Output:

$ g++ NPasswordMLength.cpp
$ a.out
 
Enter the length of the password: 3
1, 7, 4, The Passwords are: 174
147
714
741
471
417
 
Enter the length of the password: 5
1, 7, 4, 0, 9, The Passwords are: 17409
17490
17049
17094
17904
17940
14709
14790
14079
14097
14907
14970
10479
10497
10749
10794
10974
10947
19407
19470
19047
19074
19704
19740
71409
71490
71049
71094
71904
71940
74109
74190
74019
74091
74901
74910
70419
70491
70149
70194
70914
70941
79401
79410
79041
79014
79104
79140
47109
47190
47019
47091
47901
47910
41709
41790
41079
41097
41907
41970
40179
40197
40719
40791
40971
40917
49107
49170
49017
49071
49701
49710
07419
07491
07149
07194
07914
07941
04719
04791
04179
04197
04917
04971
01479
01497
01749
01794
01974
01947
09417
09471
09147
09174
09714
09741
97401
97410
97041
97014
97104
97140
94701
94710
94071
94017
94107
94170
90471
90417
90741
90714
90174
90147
91407
91470
91047
91074
91704
91740

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ 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.