UVA11332:

Summing Digits

#include <iostream>
#include <string>

using namespace std;


int solve(int x){
	if(x < 10){
		return x;
	}

	int sum = 0;
	while(x > 0){
		sum += x % 10;
		x /= 10; 
	}

	return solve(sum);
}



int main(){
	
	int x;

	while(cin >> x){
		if(x == 0)
			break;
	
		cout << solve(x) << endl;
	}

	return 0;

}
[Back to Listing Page]

Home

Posts

Projects

Github

Contact

janzzen

Developed by Janzzen Ang powered by Vercel