Skip to content

Instantly share code, notes, and snippets.

@fkztw
Created April 29, 2013 20:39
Show Gist options
  • Save fkztw/faccf2be88ab924a6091 to your computer and use it in GitHub Desktop.
Save fkztw/faccf2be88ab924a6091 to your computer and use it in GitHub Desktop.
#include <iostream>;
#include <cstdlib>;
using namespace std;
int f(int n)
{
int sum = 0;
while (n)
{
sum += n%10;
n = n/10;
}
return sum;
}
int g(int n)
{
int r = f(n);
if (1 <= r && r <= 9) return r;
else return g(r);
}
int main(void)
{
int n;
while (cin >> n)
{
if (n == 0) break;
else cout << g(n) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment