Sum of Digits
Ninja is given an integer ‘N’. One day Ninja decides to do the sum of all digits and replace the ‘N’ with the sum of digits until it becomes less than 10. Ninja wants to find what will be the value of ‘N’ after applying this operation.
Help Ninja in finding out this value.
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first and only line of the test case consists of a single integer ‘N’.
Output Format:
For each test case, print a single line containing a single integer denoting the final value of ‘N’.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= ‘T’ <= 11
1 <= ‘N’ <= 10 ^ 9
Time Limit: 1 sec.
CodingNinjas
author
2y
1-first I wrote program to add simple digit with any carry forward
2-then I moved to calculate carry forward cases
3-in that case I did mod if digit is more than 10
4-problem was I was iterating from las...read more
CodingNinjas
author
2y
Brute Force Approach
The main idea is to do the sum of digits until it becomes less than 10.
Algorithm:
- Add a condition if N is less than 10 then return N.
- Do the sum of all digits.
- Make a recursive call ...read more
CodingNinjas
author
2y
Divisibility Rule of 9 Approach
Express the given number as 9 * K +T. The divisibility rule of 9 states that if the given number is divisible by 9, then the sum of all digits is also divisible by 9. Ex...read more
Add answer anonymously...
Top Walmart Senior Software Engineer interview questions & answers
Popular interview questions of Senior Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app