Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Sum of all digits until get become a single one

Write an efficient algorithm to sum of all the digit of a result until get single digit.

Given Three method to solve this problem.

1) Iterative solution

2) recursive solution

3) Using modulus operator

Iterative solution

C program for iterative sum of all digits until get become single one.


Output

recursive solution

Output

View function execution.

Stack Areamainsum_digitauxiliary (int) =4value (int) =123sum_digitauxiliary (int) =7value (int) =12sum_digitauxiliary (int) =0value (int) =10sum_digitauxiliary (int) =0value (int) =1

Method 3: Using modulus operator

Output

Spread the post

Recommended Posts: