From 35a668f430a35c166e73c84489e85d500f458226 Mon Sep 17 00:00:00 2001 From: jatinnarula207-bot Date: Sun, 26 Oct 2025 17:25:57 +0530 Subject: [PATCH] Create digitial_root.py --- maths/digitial_root.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 maths/digitial_root.py diff --git a/maths/digitial_root.py b/maths/digitial_root.py new file mode 100644 index 000000000000..06cf8dabf4df --- /dev/null +++ b/maths/digitial_root.py @@ -0,0 +1,27 @@ +""" +digital_root.py +---------------- +Calculates the digital root of a given number. + +A digital root is obtained by summing the digits of a number repeatedly +until only a single-digit number remains. + +Example: + >>> digital_root(942) + 6 + >>> digital_root(132189) + 6 + >>> digital_root(493193) + 2 +""" + +def digital_root(n: int) -> int: + """Return the digital root of a non-negative integer.""" + while n >= 10: + n = sum(map(int, str(n))) + return n + + +if __name__ == "__main__": + num = int(input("Enter a number: ")) + print(digital_root(num))