trouble with a problem

Hello. I'm new here. I want to learn c++ programing so I started solving problems. If you don't mind, I could use some help on this one: Write a program which displays all the numbers between a and b that have the sum of their digits equal to 15, and count how many numbers of this kind are.
Here's an example to make myself clear:
let's say we enter 1 and 99.
1, is 1=15? no
2, is 2 =15? no
...
10, is 1+0=15?no
...
96, is 9+6=15? yes => counter++ and display 96 (cout)
...
99, is 9+9=15?no

And in the end the program would have to display: 69 78 87 96 and 4 (from 1 to 99 there are 4 numbers that have that property)
I even asked my teacher for some advice, and still the program didn't worked.
Here is what I've done:

#include <iostream>
using namespace std;
main ()
{
int a, b, s, aux, i, x; cin>>a>>b;
s=0; i=0; x=a;
while(x<=b)
{aux=x;
do
{
s=s+(aux%10);
aux=aux/10; }
while(aux!=0);
if(s==15)
{cout<<x<<" "; i++;}
x=x+1;

}
cout<<"number of solutions is "<<i;
}
You forgot to put
s = 0;
after aux = x;
Please use code tags and proper spacing.

The cin>>a>>b to the right of your definitions is very hard to find as is the {cout<<x<<" ";i++;}.

Also names like a and b are very hard to understand, use min and max or start and end.


Here I've fixed your code. I've changed the variable names, added comments, made your while loops into for loops. It's actually almost the same code. I'm not sure what is wrong with yours because it's rather hard to read.
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
#include <iostream>
using namespace std;
int main ()
{
	int min, max, num_matches = 0;
	cin>>min>>max; // Inputs

	// Go from min to max
	for (int i = min ; i <= max ; i++) // i is just an index here
	{
		// Add each digit
		int sum = 0; // I've moved this into the loop to ensure it's reset each time.
		for (int digits = i ; digits>0 ; digits/=10 ) // start digits at i and decrease by 10s until we are less than 0.
		{
			sum += digits%10; //Take the ones digit and add it to a running total
		}

		if (sum == 15) //If there is a match
		{
			cout << i << ", "; // Output the result
			num_matches++; // Keep track of the number of matches
		}
	}

	cout<<"number of solutions is "<< num_matches << endl;
	return 0;
}


Don't be afraid of expanding your lines to make things more readable. Sure a classmate might say, "I did it in 10 lines, how about you?" but it's not a real thing as in c++ lines are less important.

EDIT: I see the difference! I defined sum inside the first loop. You initialized it only before the first loop. This means it is never reset to zero.

Your code formatted and fixed:
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
#include <iostream>
using namespace std;
int main () //int main,  Not just main
{
	int a, b, s, aux, i, x; 

	cin>>a>>b;

	s=0; i=0; x=a;

	while(x<=b)
	{
		s = 0;//This was added.
		aux=x;
		do
		{
			s=s+(aux%10);
			aux=aux/10; 
		}while(aux>0);

		if(s==15)
		{
			cout<<x<<" "; 
			i++;
		}
		x=x+1;

	}
	cout<<"number of solutions is "<<i;
}


A few shortcuts you can use:
1
2
3
4
x=x+1;
aux=aux/10;
int a; a = 0;
s=s+(aux%10);
x++;
aux/=10;
int a=0;
s+=(aux%10);
Last edited on
Thank you, it worked. Could you tell me why I had to write s=0 in while function if I already wrote it just before?
In your variant s accumulates sum of digits of all numbers. Imagine that you defined a and b as 1 and 100, then:
if x = 1, s = 1; if x = 2, s = 1 + 2= 3; if x = 3, s = 3+3 = 6; etc.

You should compute sum of digits of each of the x.
@Stewbond Thanks, it's much easier to read now, I will do so. And to answer my question, I assume that sum didn't reset to 0 after each repetition.
Sum isn't reset to 0 automatically since the initialization was done outside of the loop.
Here is another thing. I have a problem which in order to be completed it needs to increase a number with a percentage of itself. And this simple operation is not working, so i wrote a little program separately and it doesn't work either just like down below:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
main()
{
    float x;           
    cin>>x;          // x=30
    x=x+30/100*x;  //  I want to increase x by 30%
    cout<<x;       //  x=30 is displayed
}


EDIT: I got it. It seems like I had to put a dot after the 30/100.
Last edited on
30/100 results in 0. Two integers in a quotient result in an integer. that is, any remainder is dropped.

Use 30.f/100.f to ensure that these are treated like floats and the decimal is preserved.

Alternatively you could use this to increase something by 30%
x*=1.3f;
this is the same as:
x = x * 1.3f;
this is the same as:
x = x + 0.3*x;
this is the same as:
x = x + 30.f/100.f*x;
Last edited on
Topic archived. No new replies allowed.