add digits

hi everyone ,
the following paragraph is a programming practice that i'm trying to solve but still not able to get it done :
For a positive integer n, let f(n) denote the sum of the digits of n when represented in base 10. It is easy to see that the sequence of numbers n, f(n), f(f(n)), f(f(f(n))), ... eventually becomes a single digit number that repeats forever. Let this single digit be denoted g(n).

For example, consider n = 1234567892. Then:

f(n) = 1+2+3+4+5+6+7+8+9+2 = 47
f(f(n)) = 4+7 = 11
f(f(f(n))) = 1+1 = 2
Therefore, g(1234567892) = 2.

Each line of input contains a single positive integer n at most 2,000,000,000. For each such integer, you are to output a single line containing g(n). Input is terminated by n = 0which should not be processed.


#include<iostream>
#include<string>


using namespace std;



int main()
{
int number[200];
int sum=0;

unsigned long i=0;
int temp=0;
while(cin>>number[i])
{
if(number[i]==0)
break;
while(number[i]>0)
{
sum+=number[i]%10;
number[i]/=10;
/* if(sum>10)
{
number[i]=sum;
sum=0;
}
*/
}
number[i]=sum;
i++;
/* if(sum>10)
{
number[i]=sum;
sum=0;
}*/

}
for(int j=0;j<i;j++)
{
cout<<number[j]<<endl;
}

return 0;

}
that's my code ..could someone help me please?
You g(n) part is messed up.
Here is the algorithm (pseudo-C++):
1
2
3
4
5
6
7
8
9
10
do
	Set sum to 0.
	do
		Add the last digit of n to sum (n % 10 returns the last digit of n).
		Get rid of the last digit of n (divide n by 10 to remove the last digit of n).
	while n is not 0.
	Set n to sum.
while sum does not have 1 digit only (check for this by seeing if sum divided by 10 is equal to 0).

Return sum.


Here is the function (algorithm in C++):
1
2
3
4
5
6
7
8
9
10
11
12
int g(int n) {
	int sum;
	do {
		sum = 0;
		do {
			sum += n % 10;
		} while ((n /= 10) != 0);
		n = sum;
	} while(sum / 10 != 0);

	return sum;
}


Now, all you have to do is add the user-input.
See this example of what is possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void) {
	int input;
	std::vector<int> g_of_ns;
	while(true) {
		std::cout << "What is your number: ";
		std::cin >> input;
		if (input == 0) {
			break;
		}
		g_of_ns.push_back(g(input));
	}

	std::cout << "\n\ng(n):\n";

	for (std::size_t iter = 0; iter < g_of_ns.size(); iter++) {
		std::cout << iter + 1 << ": " << g_of_ns.at(iter) << '\n';
	}

	std::cin.ignore();
	return 0;
}


Finally, here is the full, working code (note: This is an example. Please do not copy directly from it. Use this example as a way to get more ideas.):
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
#include <iostream>
#include <vector>

int g(int n) {
	int sum;
	do {
		sum = 0;
		do {
			sum += n % 10;
		} while ((n /= 10) != 0);
		n = sum;
	} while(sum / 10 != 0);

	return sum;
}

int main(void) {
	int input;
	std::vector<int> g_of_ns;
	while(true) {
		std::cout << "What is your number: ";
		std::cin >> input;
		if (input == 0) {
			break;
		}
		g_of_ns.push_back(g(input));
	}

	std::cout << "\n\ng(n):\n";

	for (std::size_t iter = 0; iter < g_of_ns.size(); iter++) {
		std::cout << iter + 1 << ": " << g_of_ns.at(iter) << '\n';
	}

	std::cin.ignore();
	return 0;
}
Last edited on
thank very much ..now i see my error ..
Actually i don't really understand the <vector> library but i will put all the result in a array and then output them and see the result ..

thank very much for your help :)
Yeah no problem! I just used vectors as a quick example... You can also use arrays like so (an example, please do not copy):
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
#include <iostream>

int g(int n) {
	int sum;
	do {
		sum = 0;
		do {
			sum += n % 10;
		} while ((n /= 10) != 0);
		n = sum;
	} while(sum / 10 != 0);

	return sum;
}

int main(void) {
	int input, size;

	std::cout << "Enter the size of your integer list: ";
	std::cin >> size;

	int data[size];

	for (int iter = 0; iter < size; iter++) {
		std::cout << "What is your number [" << iter + 1 << "]: ";
		std::cin >> input;
		data[iter] = g(input);
	}

	std::cout << "\n\ng(n):\n";

	for (int iter = 0; iter < size; iter++) {
		std::cout << iter + 1 << ": " << data[iter] << ".\n";
	}

	return 0;
}


Also, please remember to mark as solved.
Last edited on
thanks too much ..i solved it by including the algorithme in the main instead of calling the functiun g(n);
thx too much ;
hi everyone ;this is a practice that i'm trying to do but honestly i don't really get which algo i can use ..i would like to ask which algo can be proper to this one ..i will try to find the code myself ...thx!!!

Each input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Input is terminated by N = 0, which should not be processed.

For each input set, you have to print the largest possible integer which can be made by appending all the N integers.
Sample Input

Sample Output

Download Sample Input/Output
input
4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0
output
9056124123
99056124123
99999
,
Create a new thread and we can help you there.
Topic archived. No new replies allowed.