Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int takeCoefficientValue() {
- bool isIncorrect;
- int number = 0;
- cout << "Enter k value: ";
- bool isInсorrect;
- do{
- isInсorrect = false;
- cin >> number;
- if (!cin.good()){
- isInсorrect = true;
- cout << "Incorrect input!!!\nEnter k value: ";
- while (cin.get() != '\n') {
- cin.clear();
- }
- }
- if ((!isInсorrect) && (number < 1)){
- isInсorrect = true;
- cout << "The number must be more than 0\nEnter k value: ";
- }
- } while (isInсorrect);
- return number;
- }
- static int findNumberOfAnswers(int coefficient) {
- const int MAX = 1000000;
- int counter = 0;
- int current;
- int figureTotal;
- for (int i = 1; i < MAX; i++) {
- current = i;
- figureTotal = 0;
- while (current > 0) {
- figureTotal += current % 10;
- current /= 10;
- }
- if (i == figureTotal * coefficient) {
- counter++;
- }
- }
- return counter;
- }
- int *findArrayOfAnswers(int coefficient, int size) {
- int *arrayOfAnswers = new int[size];
- int MAX = 1000000;
- int counter = 0;
- int current;
- int figureTotal;
- for (int i = 1; i < MAX; i++) {
- current = i;
- figureTotal = 0;
- while (current > 0) {
- figureTotal += current % 10;
- current /= 10;
- }
- if (i == figureTotal * coefficient) {
- arrayOfAnswers[counter] = i;
- counter++;
- }
- }
- return arrayOfAnswers;
- }
- void output(int number, int* array) {
- if (number == 0) {
- cout << "Answers are not found.";
- }
- else {
- cout << "Answers are:\n";
- for (int i = 0; i < number; i++) {
- cout << array[i] << endl;
- }
- }
- }
- int main()
- {
- cout << "Welcome to the program that outputs all numbers, which are equal to the sum of their digits multiplied by k.\n";
- int coefficient = takeCoefficientValue();
- int numberOfAnswers = findNumberOfAnswers(coefficient);
- int *arrayOfAnswers = findArrayOfAnswers(coefficient, numberOfAnswers);
- output(numberOfAnswers, arrayOfAnswers);
- delete[] arrayOfAnswers;
- return 0;
- }
Add Comment
Please, Sign In to add comment