### Select Articles

Program to Find the Sum of Digits of a Given Number

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

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

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 number (Iterative approach)

C

// C program to find the sum of digits of a number

# include

int sum_of_digits(int n)

{

int sum = 0;

while (n != 0)

{

sum = sum + n ;

n = n/10;

}

return sum;

}

int main()

{

int n;

printf(“\nEnter a number : “);

scanf(“%d”,&n);

printf(“\nSum of digits of %d is %d\n”, n,sum_of_digits(n));

return 0;

}

C++

// C++ program to find the sum of digits of a number

# include

using namespace std;

int sum_of_digits(int n)

{

int sum = 0;

while (n != 0)

{

sum = sum + n ;

n = n/10;

}

return sum;

}

int main()

{

int n;

cout << “\nEnter a number : “;

cin >> n;

cout << “\nSum of digits of ” << n>

return 0;

}

JAVA 8

// Java program to find the sum of digits of a number

import java.io.*;

import java.util.*;

class Main

{

static int sum_of_digits(int n)

{

int sum = 0;

while (n != 0)

{

sum = sum + n ;

n = n/10;

}

return sum;

}

public static void main(String args[])

{

int n;

Scanner sc = new Scanner(System.in);

System.out.print(“\nEnter a number : “);

n = sc.nextInt();

System.out.print(“\nSum of digits of “+ n + ” is ” + sum_of_digits(n));

}

}

PYTHON 3

# Python program to find the sum of digits of a number

def sum_of_digits(n):

sum = 0

while (n != 0):

sum = sum + int(n )

n = int(n/10)

return sum

n = int(input(“Enter a number : “))

print(sum_of_digits(n))

OUTPUT

Enter a number : 12345

sum of digits of 12345 is : 15

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

C

// C program to find the sum of digits of a number

# include

int sum_of_digits(int n)

{

return n == 0 ? 0 : n + sum_of_digits(n/10) ;

}

int main()

{

int n;

printf(“\nEnter a number : “);

scanf(“%d”,&n);

printf(“\nSum of digits of %d is %d\n”, n,sum_of_digits(n));

return 0;

}

C++

// C++ program to find the sum of digits of a number

# include

using namespace std;

int sum_of_digits(int n)

{

return n == 0 ? 0 : n + sum_of_digits(n/10) ;

}

int main()

{

int n;

cout << “\nEnter a number : “;

cin >> n;

cout << “\nSum of digits of ” << n>

return 0;

}

JAVA 8

// Java program to find the sum of digits of a number

import java.io.*;

import java.util.*;

class Main

{

static int sum_of_digits(int n)

{

return n == 0 ? 0 : n + sum_of_digits(n/10) ;

}

public static void main(String args[])

{

int n;

Scanner sc = new Scanner(System.in);

System.out.print(“\nEnter a number : “);

n = sc.nextInt();

System.out.print(“\nSum of digits of “+ n + ” is ” + sum_of_digits(n));

} }

PYTHON 3

# Python  program to find the sum of digits of a number

def sum_of_digits(n):

return 0 if n == 0 else int(n) + sum_of_digits(int(n/10))

n = int(input(“Enter a number : “))

print(sum_of_digits(n))

OUTPUT

Enter a number : 12345

sum of digits of 12345 is : 15