Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

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 character back to an integer, and sums these integers. This sum becomes the new value of n. It also increments steps by 1.

After the loop, the function returns the final value of n (the digital root) and the number of steps taken.

The main part of the program reads an integer from input, calls digital_root_steps with this integer, and prints the results.

This problem has been solved

Similar Questions

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

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.

Single File Programming QuestionProblem StatementHelen is developing a program that analyzes numerical inputs. The program categorizes each input based on its value: a positive number elicits a specific message, a negative number prompts a different response, and inputting zero generates a unique message.Create a program that identifies the type of a number inputted. Ensure the program exits using a return statement after identifying the type of the number.Input format :The input consists of a single integer, N, which represents the number.Output format :The output displays one of the following:If the given number is positive, then display the statement "The number is positive."If the given number is negative, then display the statement "The number is negative."If the given number is zero, then display the statement "The number is zero."Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:-100 ≤ N ≤ 100Sample test cases :Input 1 :100Output 1 :The number is positive.Input 2 :-100Output 2 :The number is negative.Input 3 :0Output 3 :The number is zero.

Single File Programming QuestionProblem StatementHelen is developing a program for a gaming application that involves generating a sequence of mystical numbers based on the Tribonacci series. She needs to implement a recursive function tribonacci to determine the Tribonacci numbers for various stages of the quest. Write a program to achieve her task.The Tribonacci series is a sequence of numbers defined as the sum of the three preceding terms. 0, 1, 1, 2, 4, 7, 13, and so on.Input format :The input consists of a positive integer n.Output format :The output displays the n terms in the Tribonacci series, separated by a space.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 20Sample test cases :Input 1 :2Output 1 :0 1 Input 2 :9Output 2 :0 1 1 2 4 7 13 24 44 Input 3 :18Output 3 :0 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 Note :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.

Single File Programming QuestionProblem StatementMathew is developing a program to determine whether the digits of a given integer form an arithmetic sequence. An arithmetic sequence is a sequence of numbers in which the difference between consecutive terms is constant.Write a program that accomplishes this task using the auto keyword for local variable declarations.Example: Let us assume the number is 1234, the difference between all consecutive digits is 1, which is constant. So it forms an arithmetic sequence.Input format :The input consists of an integer N declared as an auto variable, representing the number to be checked.Output format :If the digits of N form an arithmetic sequence, the output prints "Yes".Otherwise, the output prints "No".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 106Sample test cases :Input 1 :1234Output 1 :YesInput 2 :369Output 2 :YesInput 3 :3214Output 3 :NoNote :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 : 0WhitelistSet 1:autoFill your code hereC (17)12// You are using GCCProvide Custom Input

1/3

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.