Problem Statement
A person lost his password. He remembers the number of digits as well as sum of digits
of his password. He knows that his password is the largest number of the length of Digits.
Help him retriving his password.
Example:
Input:
Length = 5, Sum = 12
Output: 93000
Example 2:
Input:
Length = 6, Sum = 23
Output = 995000
Concepts and Explanation
This program helps to recover a lost password by finding the largest number with a given length and sum of digits.
Header file: #include
The iostream
header file is part of the C++ standard library, providing functionality for input and output streams. It defines the cin
object for reading input from the console, the cout
object for writing output to the console, and other objects for working with files and strings. The endl
manipulator is also defined in this header, which is used to insert a newline character and flush the output buffer.
Using namespace std
The using namespace std;
directive is used to avoid typing std::
before every standard library object, function, or type. This allows us to write cout
instead of std::cout
, for example. This is a convenient way to reduce code verbosity.
Main Function
The main()
function is the entry point of the program, where the code execution starts. It has a return type of int
, and in this case, it takes no arguments.
Variables
Two integer variables are declared, length
and sum
, to store the length and sum of digits for the password.
Console Input and Output
We use cout
to print messages to the console, such as instructions for the user. The cin
object is used to read input from the user, in this case, the length
and sum
of the password.
For Loop
A for
loop is used to iterate through the digits of the password, from the first to the last. The loop has a local variable i
that serves as a counter.
Conditional Operator
The conditional operator (? :
) is used to check if the remaining sum
is greater than 9. If it is, the current digit is set to 9; otherwise, it's set to the remaining sum
. This helps to construct the largest possible number.
Updating the Sum
After determining the current_digit
, we subtract it from the remaining sum
to update the amount left for the remaining digits.
Printing the Current Digit
We use cout
to print the current_digit
directly to the console as the program constructs the password.
Logic of the Program
The main logic of the program is to find the largest possible number with a given length and sum of digits. To achieve this, the program follows these steps:
- Read the
length
andsum
of the password from the user. - Iterate through the digits of the password using a
for
loop with a counter variablei
from0
tolength - 1
. - At each iteration, the program checks if the remaining
sum
is greater than 9, and assigns the largest possible digit (9) to thecurrent_digit
if the condition is true. If the remainingsum
is 9 or less, the program assigns the remainingsum
to thecurrent_digit
. This ensures that the number constructed will be the largest possible number with the givensum
. - After determining the
current_digit
, the program subtracts it from the remainingsum
to update the amount left for the remaining digits. - The program then prints the
current_digit
directly to the console as it constructs the password. - Once the loop has finished iterating through all the digits, the program prints a success message, indicating that the password has been successfully retrieved.
By following this logic, the program can efficiently find the largest possible number with the given length
and sum
of digits.
Ending the Program
Once the loop has finished iterating through all the digits, the program prints a success message and returns 0
, indicating that the program has executed successfully.
Method 1
Code
/*
* -----------------------------------------------------------
* OOPS using C++ Language (CSU1287P)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <iostream>
using namespace std;
int main()
{
int length, sum;
cout << "This program helps you recover your lost password by finding the largest number of given length and sum of digits, using object-oriented programming concepts and algorithms in C++. \n";
// Prompt the user to enter the length and sum of digits
cout << "Enter the length of the password and sum of digits - (Example: 99200 = 5 20) - and press Enter: ";
cin >> length >> sum;
cout << "\n\tYour password \"";
// Loop through the password's digits
for (int i = 0; i < length; i++)
{
// Check if the remaining sum is greater than 9; if so, assign 9 to the current digit
// Otherwise, assign the remaining sum to the current digit
int current_digit = sum > 9 ? 9 : sum;
// Subtract the current digit from the remaining sum
sum -= current_digit;
// Output the current digit
cout << current_digit;
}
// Output message after finding the password
cout << "\" has been retrieved successfully." << endl;
return 0;
}
Method 2: Using While Loop
#include <iostream>
using namespace std;
int main() {
int d, m;
cout << "Enter the length: ";
cin >> d;
cout << "Enter the sum: ";
cin >> m;
int pos = 0;
cout << "Output: ";
while (pos < d) {
int curr = m > 9 ? 9 : m;
m -= curr;
cout << curr;
pos++;
}
cout << endl;
return 0;
}
Method 3: Using a string to store the result
#include <iostream>
#include <string>
using namespace std;
int main() {
int len, sum;
cout << "Enter the length of the password: ";
cin >> len;
cout << "Enter the sum of the digits of the password: ";
cin >> sum;
string password = "";
for (int i = 0; i < len; i++) {
int curr = sum > 9 ? 9 : sum;
sum -= curr;
password += to_string(curr);
}
cout << "Generated password: " << password;
return 0;
}
Method 4: Using a recursive function
#include <iostream>
using namespace std;
void find_password(int len, int sum) {
// Base case: length of password is 0
if (len == 0) {
return;
}
// Find the largest possible digit for the current position
int curr_digit = sum > 9 ? 9 : sum;
sum -= curr_digit;
// Recursively find the rest of the password
find_password(len - 1, sum);
// Output the current digit
cout << curr_digit;
}
int main() {
int len, sum;
// Take input for length and sum of digits
cout << "Enter the length of the password: ";
cin >> len;
cout << "Enter the sum of the digits in the password: ";
cin >> sum;
// Find and output the password
cout << "The password is: ";
find_password(len, sum);
return 0;
}
Method 5: Using an integer array to store the result
#include <iostream>
using namespace std;
int main() {
int len, sum;
cout << "Please enter the length and sum: ";
cin >> len >> sum;
int pw[len];
for (int i = 0; i < len; i++) {
int current_digit = sum > 9 ? 9 : sum;
sum -= current_digit;
pw[i] = current_digit;
}
cout << "Password: ";
for (int i = 0; i < len; i++) {
cout << pw[i];
}
cout << endl;
return 0;
}
Method 6: Using a function to find the largest number
#include <iostream>
#include <string>
using namespace std;
// function to find password based on given length and sum
string findPassword(int len, int s) {
string password = "";
for (int i = 0; i < len; i++) {
// current digit should be maximum 9 or equal to sum if sum < 10
int currentDigit = (s > 9) ? 9 : s;
s -= currentDigit;
password += to_string(currentDigit);
}
return password;
}
int main() {
int len, s;
// take input from user for length and sum
cout << "Enter length and sum: ";
cin >> len >> s;
// print the password after finding it using findPassword function
cout << "Password is: " << findPassword(len, s);
return 0;
}
Method 7: Using string stream
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int len, sum;
cout << "Enter the length of the password: ";
cin >> len;
cout << "Enter the sum of digits of the password: ";
cin >> sum;
stringstream pwd;
for (int i = 0; i < len; i++) {
int curr = sum > 9 ? 9 : sum;
sum -= curr;
pwd << curr;
}
cout << "The password is: " << pwd.str() << endl;
return 0;
}
Method 8: Using a function that returns a character
#include <iostream>
using namespace std;
char getDigit(int& s) {
int d = s > 9 ? 9 : s;
s -= d;
return '0' + d;
}
int main() {
int len, sum;
cout << "Enter the length: ";
cin >> len;
cout << "Enter the sum: ";
cin >> sum;
cout << "Output: ";
for (int i = 0; i < len; i++) {
cout << getDigit(sum);
}
cout << endl;
return 0;
}
Method 9: Using a lambda function
#include <iostream>
using namespace std;
int main() {
int len, s;
cout << "Enter length and sum: ";
cin >> len >> s;
auto get_digit = [&s]() {
int digit = s > 9 ? 9 : s;
s -= digit;
return digit;
};
cout << "Generated sequence of digits: ";
for (int i = 0; i < len; i++) {
cout << get_digit();
}
cout << endl;
}
Method 10: Iterating in reverse order
#include <iostream>
#include <string>
using namespace std;
int main() {
int len, total_sum;
cout << "Enter the length of the password: ";
cin >> len;
cout << "Enter the total sum of digits in the password: ";
cin >> total_sum;
string password(len, '0');
int pos = len - 1;
while (total_sum > 0) {
int current_digit = total_sum > 9 ? 9 : total_sum;
total_sum -= current_digit;
password[pos--] = '0' + current_digit;
}
cout << "The generated password is: " << password << endl;
return 0;
}
Method 11: Using a for loop to print the password
#include <iostream>
using namespace std;
int main() {
int len, sum;
cout << "Enter the length of the password: ";
cin >> len;
cout << "Enter the sum of digits in the password: ";
cin >> sum;
int password[len];
for (int i = 0; i < len; i++) {
int current_digit = sum > 9 ? 9 : sum;
sum -= current_digit;
password[i] = current_digit;
}
cout << "The password is: ";
for (int digit : password) {
cout << digit;
}
cout << endl;
return 0;
}
Method 12: Using a do-while loop
#include <iostream>
using namespace std;
int main() {
// Input length and sum of digits
int len, sum;
cout << "Enter the length and sum of digits: ";
cin >> len >> sum;
// Find the digits
int pos = 0;
do {
int cur_digit = sum > 9 ? 9 : sum;
sum -= cur_digit;
cout << cur_digit;
pos++;
} while (pos < len);
// Output the found digits
cout << "\nThe digits are: ";
for (int i = 0; i < len; i++) {
cout << 9;
}
cout << endl;
return 0;
}
Method 13: Using a vector to store the result
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Prompt user to enter length and sum of the password
cout << "Enter the length and sum of the password: ";
// Read the length and sum of the password from user input
int len, s;
cin >> len >> s;
// Generate the password using the given length and sum
vector pw(len);
for (int i = 0; i < len; i++) {
int cur_digit = s > 9 ? 9 : s;
s -= cur_digit;
pw[i] = cur_digit;
}
// Display the generated password
cout << "The generated password is: ";
for (int digit : pw) {
cout << digit;
}
cout << endl;
return 0;
}
Method 14: Using a function with a reference parameter
#include <iostream>
using namespace std;
// A helper function that adds a digit to the password
void addDigitToPassword(int &sum, ostream &out) {
int currentDigit = sum > 9 ? 9 : sum; // add no more than 9 to the password
sum -= currentDigit;
out << currentDigit; // print the added digit
}
int main() {
int length, sum;
cout << "Enter the length of the password: ";
cin >> length;
cout << "Enter the sum of digits of the password: ";
cin >> sum;
cout << "Password: ";
for (int i = 0; i < length; i++) {
addDigitToPassword(sum, cout);
}
cout << endl; // print a newline at the end
return 0;
}
Method 15: Using a function with a pointer parameter
#include <iostream>
using std::cin;
using std::cout;
void addDigitToPassword(int *sum, std::ostream *out) {
int currentDigit = *sum > 9 ? 9 : *sum;
*sum -= currentDigit;
*out << currentDigit;
}
int main() {
int length, sum;
cout << "Enter the length of the password: ";
cin >> length;
cout << "Enter the sum of the digits of the password: ";
cin >> sum;
cout << "Generated password: ";
for (int i = 0; i < length; i++) {
addDigitToPassword(&sum, &cout);
}
cout << "\n";
return 0;
}
Bonus Method: Using C++20's std::format
#include <iostream>
#include <string>
#include <format>
using namespace std;
int main() {
// Prompt user for input
cout << "Enter the length and sum: ";
// Take input from user
int len, sum;
cin >> len >> sum;
// Generate password
string password = "";
for (int i = 0; i < len; i++) {
int curr = sum > 9 ? 9 : sum;
sum -= curr;
password += format("{}", curr);
}
// Output generated password
cout << "Generated Password: " << password << endl;
return 0;
}
Output for Method 1:
This program helps you recover your lost password by finding the largest number of given length and sum of digits, using object-oriented programming concepts and algorithms in C++.
Enter the length of the password and sum of digits - (Example: 99200 = 5 20) - and press Enter: 6 18
Your password "990000" has been retrieved successfully.