Skip to main content
Back to problems
#258
Easy Algorithms

Add digits

Math Simulation Number Theory
68.6% acceptance
Jan 12, 2026
5465
1978
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn add_digits(num: i32) -> i32 {
    if num == 0 {
      0
    } else if num % 9 == 0 {
      9
    } else {
      num % 9
    }
  }
}