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

Algorithm :
1. Declare a variable to store the sum and set it to 0
2. Repeat the next two steps till the number is not 0
3. Get the rightmost digit of the number with help of the remainder ‘%’ operator ...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...
Morgan Stanley Technology Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter