Topic description
jhljx is a person who likes mathematics very much. He is proficient in all kinds of mathematics above elementary mathematics, and he is very proficient in all kinds of integrals. .
But his only flaw is that he can't do addition and subtraction. .
So, he could only go back to primary school in silence. . Orz. . His primary school teacher is Luxaky Luee.
After LuxakyLuee knew about this, he said it was a disease and had to be cured. .
LuxakyLuee doesn't let him do ordinary addition and subtraction, because special treatment is required for special patients.
LuxakyLuee gave jhljx a number and asked him to add up each digit of the number.
If the sum has more than 1 digit, continue to add up each digit of the number until there is only one at the end.
In this way, jhljx can perform many additions. He said he was very happy.
enter
Enter a set of data.
This set of data has only one row, which is an integer n.
(Guaranteed 33.33% of n is in int range, 33.33% of n is in long long range, 33.33% of n is over long long range)
output
Output the final result.
sample input
987
Sample output
6
AC code
#include <stdio.h>
#define MAX 100000000
char arr[MAX];
int main()
{
int ans = 0;
scanf("%s", arr);
for (int i = 0; arr[i] != '\0'; i++)
ans += arr[i] - '0';
while (ans > 9) {
int tmp = ans;
ans = 0;
while (tmp > 0) {
ans += tmp % 10;
tmp /= 10;
}
}
printf("%d\n", ans);
return 0;
}