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

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)
  1. Get the rightmost digit of the number using ‘%’ operator by dividing it with 10 and add it to sum
  2. Divide the number by 10.
  • Return sum

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

// 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 % 10;
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++ program to find the sum of digits of a number

# include <iostream>
using namespace std;

int sum_of_digits(int n)
{
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}

int main()
{
int n;
cout << “\nEnter a number : “;
cin >> n;
cout << “\nSum of digits of ” << n << ” is ” << sum_of_digits(n) << endl;
return 0;
}

// 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 % 10;
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 program to find the sum of digits of a number

def sum_of_digits(n):
sum = 0
while (n != 0):

sum = sum + int(n % 10)
n = int(n/10)
return sum

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

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

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

# include<stdio.h>

int sum_of_digits(int n)
{
return n == 0 ? 0 : n%10 + 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++ program to find the sum of digits of a number

# include <iostream>
using namespace std;

int sum_of_digits(int n)
{
return n == 0 ? 0 : n%10 + sum_of_digits(n/10) ;
}

int main()
{
int n;
cout << “\nEnter a number : “;
cin >> n;
cout << “\nSum of digits of ” << n << ” is ” << sum_of_digits(n) << endl;
return 0;
}

// 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%10 + 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  program to find the sum of digits of a number

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

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

Output:

sum of digits of a number