Program to find the sum of digits of a given number | faceprep

Program to find the sum of digits of a given number | faceprep

The program to find the sum of the digits of the given number is discussed here. This problem can be solved in two methods: The iterative approach and the Recursive approach

For example, let the input number be 719. The sum of digits of 719 = 7 + 1 + 9 = 17



program-to-find-the-sum-of-digits-of-a-given-number


Algorithm to find the sum of digits of a number

  • Input the number.
  • Declare a variable called sum and initialize it to 0.
  • while (number > 0)
  • Get the rightmost digit of the number using % operator by dividing it with 10 and add it to sum
  • Divide the number by 10.
  • Return sum



program-to-find-the-sum-of-digits-of-a-given-number

Program to find the sum of digits of a number (Iterative approach)

@@coding::1@@


Program to find the sum of digits of a number (Recursive approach)

@@coding::2@@


Recommended Programs










Program to Find the Sum of Digits of a Given Number

c