loops, loops, loops

It's supposed to find the numbers for which their digits' sum equals their digits' product. For example, taking the number 123: 1+2+3=6 and 1*2*3=6. So the program has to find and display the first 20 such numbers.

The code bellow runs perfectly except one thing. It displays ALL such numbers. I don't know how to make it stop after displaying the first 20.
I have tried including the main function's content in for and do/while loops but I've had no result so far.

Any ideas on how to make it stop after displaying the first 20 numbers in which the digit sum equals the product?
Much appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <iostream>
using namespace std;

int somme ( int n);
int produit( int n);
bool somme_produit_egaux( int n);

int main()
{
	int n(11);
	while ( n > 10)
	{
		if (somme_produit_egaux(n))
		{
			cout << n << ", ";
		};
	n++;
};

	return 0;
}

int somme ( int n)
{
	int r, answer(0);
	while(n != 0)
	{
		r = n % 10;
		n = n / 10;
		answer = answer + r;
	}
	
	return answer;
}

int produit( int n)
{
	int r, answer(1);
	while( n != 0)
	{
		r = n % 10;
		n = n / 10;
		answer = answer * r;
	}
	return answer;
}

bool somme_produit_egaux( int n)
{
	return somme(n) == produit(n);
}
Last edited on
There are some changes. Try to understand them and modify the program as you wish.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
  
using namespace std;

int somme ( int n);
int produit( int n);
bool somme_produit_egaux( int n);

int main()
{
	int n(11);
	int contor = 0;
	while ( n > 10)
	{
		if (somme_produit_egaux(n))
		{
			++contor;
			cout << setw(4) << contor << setw(8) << n << '\n';
		}
		if(contor > 19)return 0;
	    n++;
	}
	return 0;
}

int somme ( int n)
{
	int r, answer(0);
	while(n != 0)
	{
		r = n % 10;
		n = n / 10;
		answer = answer + r;
	}
	return answer;
}

int produit( int n)
{
	int r, answer(1);
	while( n != 0)
	{
		r = n % 10;
		n = n / 10;
		answer = answer * r;
	}
	return answer;
}

bool somme_produit_egaux( int n)
{
	return somme(n) == produit(n);
}
   1      22
   2     123
   3     132
   4     213
   5     231
   6     312
   7     321
   8    1124
   9    1142
  10    1214
  11    1241
  12    1412
  13    1421
  14    2114
  15    2141
  16    2411
  17    4112
  18    4121
  19    4211
  20   11125

Last edited on
Thank you so much!
I'm not sure what 'setw' is all about (probably something I haven't studied yet) but your code still gave me the solution!

And it was so simple. I feel embarrassed!

Thanks again!
You're welcome!

setw() sets the field width to be used on output operations.
setw() is part of the C++ <iostream> library that allows you to format the output of cout. What it does is tell cout to print the output to take up a certain amount of space, regardless of how many digits/characters are actually output.
Topic archived. No new replies allowed.