Question

Need a program that outputs all possible combinations of X amount of numbers depending on the...

Need a program that outputs all possible combinations of X amount of numbers depending on the length of the password. For example it will ask the user the length of the password and if they input 3 then it will display all possible combinations from numbers 0 through 9 so the sample output will look like.

000, 001, 002, 003, 004, 005, 006,

.....................................................

994, 995, 996, 997, 998, 999

after it will ask the user if they want to search for a number. It will ask the user what numbers they want to search through and will display them for example.

How many numbers do you want to input? 2.

Enter the numbers you would like to display that start with those 2 numbers? 56. Then it will display all possible numbers starting with 56.

output example

5600, 5601, 5602, ..........., 5697, 5698, 5699

if the user chooses 1 number to input starting with the number 5 then output would be like:

5001, 5002, 5003, ..........., 5999

and depending on the length of the password it will display more numbers the max they could enter is 9 for the length. The max for searching a number should be equal to the length or less than the length and greater than zero.

need the code to be written in C++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<iostream>
#include <string.h>
using namespace std;

int main()
{
   int length;
   char pass[10];
   cout<<endl<<"Enter the length of password: ";
   cin>>length;
   if(length >= 1 && length <= 9)
   {
       strcpy(pass,"000000000");
       int i = length-1;
       pass[length] = '\0';
       cout<<endl<<pass;
       while(1)
       {
           if( pass[i]-48 < 9 )
           {
               pass[i] += 1;
           }
           else
           {
               int j = length-1;
               while( j>= 0 )
               {
                   if( pass[j]-48 < 9 )
                   {
                       pass[j] += 1;
                       break;
                   }
                   else
                   {
                       pass[j] = '0';
                       j--;
                   }
               }
               if( j < 0 )
                   break;
           }
           cout<<","<<pass;
       }
       cout<<endl;
      
       int num_len;
       char num_pass[10];
      
       cout<<endl<<"How many number you want to input? ";
       cin>>num_len;
      
       if(num_len >= 1 && num_len <= length)
       {
           cout<<endl<<"Enter the numbers you would like to display that start with those "<<num_len<<" numbers? ";
           cin>>num_pass;
           if( length == num_len )
               cout<<num_pass;
           else
           {
               strcpy(pass,"000000000");
               i = length-1;
               pass[length] = '\0';
               strcpy(pass,num_pass);
               pass[num_len] = '0';
               cout<<endl<<pass;
               while(1)
               {
                   if( pass[i]-48 < 9 )
                   {
                       pass[i] += 1;
                   }
                   else
                   {
                       int j = length-1;
                       while( j>= num_len )
                       {
                           if( pass[j]-48 < 9 )
                           {
                               pass[j] += 1;
                               break;
                           }
                           else
                           {
                               pass[j] = '0';
                               j--;
                           }
                       }
                       if( j < num_len )
                           break;
                   }
                   cout<<","<<pass;
               }
           cout<<endl<<endl;
           }
       }
       else
       {
           cout<<endl<<"Enter Number between 1 and length of password"<<endl<<endl;
       }
   }
   else
   {
       cout<<endl<<"Enter Number between 1 and 9"<<endl<<endl;
   }
}


Execution and Output:

Add a comment
Know the answer?
Add Answer to:
Need a program that outputs all possible combinations of X amount of numbers depending on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program called SmallestLargest.java which outputs the biggest and smallest numbers in a list of...

    Write a program called SmallestLargest.java which outputs the biggest and smallest numbers in a list of numbers entered by the user. Ask the user for a terminating value which should be entered again when they are done inputting the list of numbers. First output the biggest number and then the smallest number. There must be at least 1 number in the list. YOU MUST USE THE IO MODULE FOR INPUT/OUTPUT. Report bad input via IO.reportBadInput() and exit on error. Example:...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • MATLAB QUESTION 8) Create a program that first prompts the user to enter any two numbers....

    MATLAB QUESTION 8) Create a program that first prompts the user to enter any two numbers. Then prompt the user again to input as many numbers as they choose. Stop the program by inputting-1. Then, display the largest number and second largest number in the command window once the user kills the program. The built-in max function cannot be used. The figure below is an example of what the command window should look like when the code is ran. First...

  • I need help with this in C++ Program 2) Display and sum up all the numbers...

    I need help with this in C++ Program 2) Display and sum up all the numbers of factor of 5 in a given range. a) Request a number range (min/max values) separated by a space. b) Use a validation loop to ensure that both max and min are integers and max is greater than min. c) Output all the numbers of factor of 5 between the max and min. d) Output the sum of all the displayed numbers. e) Output...

  • I need to create a bit of code for javascript which I can have random numbers...

    I need to create a bit of code for javascript which I can have random numbers generate with a certain amount of digits set by the user. and ask the user how many times they want this to happen. For example The user says they want a number with 4 digits (any number between 0 - 9999) and the user can input if they want to add, subtract, multiply, divide, or make it random 2 random numbers are generated. First...

  • In C++ Programming Write a program in a file named SeedFinder.cpp that finds and displays all...

    In C++ Programming Write a program in a file named SeedFinder.cpp that finds and displays all the seeds of a integer given by the user. Let’s define a seed of an integer, n, to be a positive value that equals n when multiplied by its individual digits. For example, 23 is a seed of 138 because 23 × 2 × 3 = 138. 111 is an example of a seed of itself because 111 × 1 × 1 × 1...

  • Write one single program that does all of the following tasks, in order. You should practice...

    Write one single program that does all of the following tasks, in order. You should practice proper Top Down Design and Procedural Abstraction techniques unless otherwise noted. -Write a program with the following functions, with the correct number of arguments for each function. -void fillUpArray ( argument1, argument2) This function should read in a text file called "villagers.txt" (You will create your own). "villagers.txt" is a text file of numbers in ascending order, ranging anywhere from 1 to 100, nonrepeating....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT