Input Format:
The input contains an integer which denotes 'n'
Output Format:
Print the single digit number
Sample Input
88
Sample Output
7
Explanation:
Step 1: 8+8 = 16
Step 2: 1+6 = 7
sum of digits of a number until it becomes a single-digit number which is 7 here.
Solution:
#include<iostream>
using namespace std;
int singleDigit(int n){
int r = 0, s = 0;
while(n > 9){
while(n > 0){
r = n % 10;
n = n / 10;
s += r;
}
n = s;
s = 0;
}
return n;
}
int main(){
int n;
cin>>n;
cout<<singleDigit(n);
}
No comments:
Post a Comment