Math, asked by Manukrishnan1448, 11 months ago

Given an integer, find sum of digits of that number until sum becomes single digit

Answers

Answered by heetgandhi820
0

Answer:

Finding sum of digits of a number until sum becomes single digit

Given a number n, we need to find the sum of its digits such that:

If n < 10

digSum(n) = n

Else

digSum(n) = Sum(digSum(n))

Examples :

Input : 1234

Output : 1

Explanation : The sum of 1+2+3+4 = 10,

digSum(x) == 10

Hence ans will be 1+0 = 1

Input : 5674

Output : 4

Answered by anagasatyasri710
1

Answer:

#include<iostream>  

using namespace std;  

int digSum(int n)  

{  

int sum = 0;  

while(n > 0 || sum > 9)  

{  

if(n == 0)  

{  

n = sum;  

sum = 0;  

}  

sum += n % 10;  

n /= 10;  

}  

return sum;  

}  

int main()  

{  

int n;

cin>>n;

cout << digSum(n);  

return 0;  

}

Step-by-step explanation:

Similar questions