what operator to use?

i am having trouble figuring out how to get the last part of my output. i already figured out how to count the individual integer digits and use modulus to sum up the digits. now my problem is that i don't know how to ask the program to count the number of digits "n" the input has and see how many "n" is included in the input.

i.e.

the entered value is "2677137" it has 7 digits with the digit sum of 33
the number of sevens in the input is 3.


i need the last line of output. any type of help is appreciated! thank you in advance


here is what i have so far
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>
#include <cmath>
#include <conio.h>

using namespace std;


int digitsum(int& value, int& sum);

int main()
{

int value, digits, sum;

		cout<< "enter value here: "<< endl;
		cin>> value;
	
	if (value == 0)
		{return 0;}
	digitsum (value, sum);

digits=floor(log10(value))+1;

		cout<< " the value entered is "<< value << " it has " << digits << " digits "<< " with the digit sum of " << sum << endl;

system ("pause");
return 0;
}


int digitsum(int& value, int& sum)
{
	int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;

num1 = value / 1000000000;
num2 = value % 1000000000 / 100000000;
num3 = value % 1000000000 % 100000000 / 10000000;
num4 = value % 1000000000 % 100000000 % 10000000 / 1000000;
num5 = value % 1000000000 % 100000000 % 10000000 % 1000000 / 100000;
num6 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 / 10000;
num7 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 / 1000;
num8 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 / 100;
num9 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 / 10;
num10 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 % 10 /1;

	sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
	 
return (sum);


}
Are you only counting sevens? Or is it dependent on the number of digits the number is? IE in your example, "2677137" has 7 digits so you're counting the number of 7's?

Anyway, if you're only talking about sevens, you could add this to your digitsum function:

1
2
3
4
5
6
7
8
9
10
11
int countSevens = 0;
if (num1 == 7) countSevens++;
if (num2 == 7) countSevens++;
if (num3 == 7) countSevens++;
if (num4 == 7) countSevens++;
if (num5 == 7) countSevens++;
if (num6 == 7) countSevens++;
if (num7 == 7) countSevens++;
if (num8 == 7) countSevens++;
if (num9 == 7) countSevens++;
if (num10 == 7) countSevens++;


This would all be a lot simpler if you used a loop in digitsum instead of what you have right now.
he is counting the over all total digits (In my understanding)
Last edited on
thanks for the response, but the number is dependent on the number of digits that is entered by the user.
Try this:
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
55
56
57
58
59
60
#include <iostream>
#include <cmath>
#include <conio.h>
#include <windows.h>
using namespace std;
int num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0, num8=0, num9=0, num10=0,value,digits,sum;
int digitcount();
int digitsum();

int main()
{

		cout<< "enter value That isn't zero here: "<< endl;
		cin>> value;

	if (value == 0)
		{return 0;}
	digitsum ();

digits=floor(log10(value))+1;

cout<< " the value entered is "<< value << " it has " << digits << " digits "<< " with the digit sum of " << sum << endl;

system ("pause");
return 0;
}


int digitsum()
{
num1 = value / 1000000000;
num2 = value % 1000000000 / 100000000;
num3 = value % 1000000000 % 100000000 / 10000000;
num4 = value % 1000000000 % 100000000 % 10000000 / 1000000;
num5 = value % 1000000000 % 100000000 % 10000000 % 1000000 / 100000;
num6 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 / 10000;
num7 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 / 1000;
num8 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 / 100;
num9 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 / 10;
num10 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 % 10 /1;

	sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;

return (sum);


}
int digitcount()
{
if(num1!=0) digits++;
if(num2!=0) digits++;
if(num3!=0) digits++;
if(num4!=0) digits++;
if(num5!=0) digits++;
if(num6!=0) digits++;
if(num7!=0) digits++;
if(num8!=0) digits++;
if(num9!=0) digits++;
if(num10!=0) digits++;
}
Last edited on
had to add a return value and a cout for digit count, and it worked flawlessly! thank you very much!
Are you sure that worked for you? That digitcount function does the same thing as digits=floor(log10(value))+1; and it doesn't even set digits = 0 before it increments.

I was hoping you'd take my hint and create a loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void processNumber(unsigned int num, unsigned int &digitCount, unsigned int &digitSum, unsigned int &nCount)
{
    unsigned int remainder;
    digitCount = floor(log10(num)) + 1;
    digitSum = nCount = 0;
    while (num > 0)
    {
        remainder = num % 10;
        digitSum += remainder;
        if (remainder == digitCount)
            nCount++;
        num /= 10;
    }
}
Topic archived. No new replies allowed.