Imagine Mary is developing a program to calculate the digital root of a positive integer. The digital root of a positive integer is the number obtained by repeatedly summing the digits until a one-digit sum is obtained. For example, for the number 873597, the digital root is calculated in the following steps:-> 8 + 7 + 3 + 5 + 9 + 7 = 39 -> 3 + 9 = 12 -> 1 + 2 = 3 Write a program that will read in a positive integer and write both its digital root and the number of steps required to obtain it.Note: This question was asked in ACM-ISPC contest.Input format :The input consists of a positive integer n.Output format :The first line displays "Digital Root: " followed by the digital root of n.The second line displays "Number of Steps: " followed by the number of steps required to reach a single-digit digital root.Refer to the sample output for the formatting specificationsCode constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ n ≤ 106
Question
Imagine Mary is developing a program to calculate the digital root of a positive integer. The digital root of a positive integer is the number obtained by repeatedly summing the digits until a one-digit sum is obtained. For example, for the number 873597, the digital root is calculated in the following steps:-> 8 + 7 + 3 + 5 + 9 + 7 = 39 -> 3 + 9 = 12 -> 1 + 2 = 3 Write a program that will read in a positive integer and write both its digital root and the number of steps required to obtain it.Note: This question was asked in ACM-ISPC contest.Input format :The input consists of a positive integer n.Output format :The first line displays "Digital Root: " followed by the digital root of n.The second line displays "Number of Steps: " followed by the number of steps required to reach a single-digit digital root.Refer to the sample output for the formatting specificationsCode constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ n ≤ 106
Solution
Sure, here is a Python solution for the problem:
def digital_root_steps(n):
steps = 0
while n > 9:
n = sum(int(digit) for digit in str(n))
steps += 1
return n, steps
n = int(input())
root, steps = digital_root_steps(n)
print("Digital Root: ", root)
print("Number of Steps: ", steps)
This program works by defining a function digital_root_steps that takes an integer n as input. It initializes a counter steps at 0, then enters a loop that continues as long as n is greater than 9. Inside the loop, it converts n to a string, iterates over each character (digit) in the string, converts each digit back to an integer, and sums them. This sum becomes the new value of n. It also increments steps by 1 each time through the loop. When n is no longer greater than 9, the function returns n (the digital root) and steps.
The main part of the program reads an integer from input, calls digital_root_steps with this integer, and prints the results.
Similar Questions
Single File Programming QuestionProblem StatementImagine Mary is developing a program to calculate the digital root of a positive integer. The digital root of a positive integer is the number obtained by repeatedly summing the digits until a one-digit sum is obtained. For example, for the number 873597, the digital root is calculated in the following steps:-> 8 + 7 + 3 + 5 + 9 + 7 = 39 -> 3 + 9 = 12 -> 1 + 2 = 3 Write a program that will read in a positive integer and write both its digital root and the number of steps required to obtain it.Note: This question was asked in ACM-ISPC contest.Input format :The input consists of a positive integer n.Output format :The first line displays "Digital Root: " followed by the digital root of n.The second line displays "Number of Steps: " followed by the number of steps required to reach a single-digit digital root.Refer to the sample output for the formatting specificationsCode constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ n ≤ 106Sample test cases :Input 1 :123Output 1 :Digital Root: 6Number of Steps: 1Input 2 :2Output 2 :Digital Root: 2Number of Steps: 0Input 3 :873597Output 3 :Digital Root: 3Number of Steps: 3Input 4 :1000000Output 4 :Digital Root: 1Number of Steps: 1
Design a program that mimics a digital calculator. Take an integer as input, use a function named sumOfDigits with a pointer to calculate the sum of its digits, and display the result. This simulates the process of manually adding up the individual digits of a number as you would on a calculator.Note: This question helps in clearing Wipro technical coding tests.Input format :The input consists of an integer N.Output format :The output prints an integer representing the sum of digits of N.Code constraints :1 ≤ N ≤ 105Sample test cases :Input 1 :123Output 1 :6Input 2 :5698Output 2 :28Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0
James, a mathematics teacher, is developing a programming exercise to help his students practice continuously summing the digits of a number until it becomes a single-digit integer. He wants to create a simple program using a 'while' loop that takes a positive integer input and generates the final single-digit result. Input format :The input consists of a positive integer n.Output format :The output prints "The single-digit sum of n is Y." where n is the input integer and Y is its single-digit sum.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 1010Sample test cases :Input 1 :456Output 1 :The single-digit sum of 456 is 6.Input 2 :999Output 2 :The single-digit sum of 999
Single File Programming QuestionProblem StatementOlivia is a curious mind exploring the world of digits. Create a simple program to assist Olivia in understanding the addition of the last two digits of a given number. Prompt Olivia to input an integer n, calculate, and display the sum of the last two digits.Input format :The input consists of an integer n.Output format :The output displays the sum of the last two digits of the input integer.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:10 ≤ n ≤ 105Sample test cases :Input 1 :10Output 1 :1Input 2 :231Output 2 :4Input 3 :7896Output 3 :15Input 4 :100000Output 4 :0Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Maria, an explorer in the digital world, needs a program to set the Nth bit of an integer. Write a program that takes an integer M and a position N as input, sets the Nth bit to 1, and displays the updated value.Example: If Maria inputs M = 18 and N = 3, the binary representation of 18 is 10010. After setting the 3rd bit (0-based) to 1, the updated value is 11010, which is 26. Input format :The input consists of two space-separated integers M and N, representing the number and the position, respectively.Output format :The output displays a single integer, representing the updated value of M after setting the Nth bit to 1.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:10 ≤ M ≤ 10001 ≤ N ≤ 5
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.