Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Add Digits problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Add Digits problem solution, we have given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Leetcode Add Digits problem solution

Leetcode Add Digits problem solution in Python.

class Solution:
    def addDigits(self, num: int) -> int:
        while num > 9:
            num = num%10+ num//10
        return num

Add Digits problem solution in Java.

class Solution {
public int addDigits(int num) {
while(num>=10)
{
int rem=num%10;
int q=num/10;
int res=rem+q;
num=res;
}
return num;

}
}

Problem solution in C++.

class Solution {
public:
    int addDigits(int num) {
     int sum=0;
        while(num>0)
        {
            int last=num%10;
            sum+=last;
            num=num/10;
            if(num==0 && sum>9)
            {
                num=sum;
                sum=0;
            }
        }
        return sum;
    }
};

Problem solution in C.

int addDigits(int num){
    
    while(num/10){
        int sum = 0;
        while(num){
            sum += num%10;
            num /= 10;
        }
        num = sum;
    }
    return num;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes