 # How to find the Sum of Digits in Java?

Published on Oct 22,2019 198 Views 4 / 29 Blog from Java Programs

Java is a versatile programming language. There are various methods through which we can calculate the sum of digits in Java. Let us discuss those methods in detail. Below given is a list which we would use to solve our problem.

Lets begin!

## By using for loop

Programmers usually use loops to execute a set of statements. For loop is used when they need to iterate a part of the programs multiple times. It is particularly used in cases where the number of iterations is fixed!

Code:

```
class SumOfDigits
{
public static void main(String arg[])
{
long n,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
n=sc.nextLong();
for(sum=0 ;n!=0 ;n/=10)
{
sum+=n%10;
}
System.out.println("Sum of digits "+sum);
}
}

```

Output:
Enter a number
456
Sum of digits
15

The next segment tell you how to use static method to accomplish the goal.

## By using static method

In Java, a static method is a method that belongs to a class rather than an instance of a class. The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that member of a class.

Code:

```
class SumOfDigits
{
public static void main(String arg[])
{
long n,s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
n=sc.nextLong();
s=sum(n);
System.out.println("Sum of digits "+s);
}
static int sum(long num)
{
int sum=0;
while(num!=0)
{
sum+=num%10;
num/=10;
}
return sum;
}
}

```

Output:
Enter a number
678
Sum of digits
21

Let us now move to another method that is recursion.

## By using recursion

Code:

```
import java.util.Scanner;
class SumOfDigits

{
int sum=0;
int sum(long num)
{

if(num!=0)
{
sum+=num%10;
num/=10;
sum(num);
}
return sum;
}

public static void main(String arg[])
{
long n,res;
SumOfDigits s=new SumOfDigits();
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
n=sc.nextLong();

System.out.println("Sum of digits "+s.sum(n));

}
}

```

Output:
Enter a number
345
Sum of digits
12

Lastly, we will use command line arguments to calculate the sum of the digits of a number of a number.

## By using command line arguments

Code:

```
class SumOfDigits
{
public static void main(String arg[])
{
long n,sum=0;
System.out.println("Enter a number ");
n=Long.parseLong(arg);
while(n!=0)
{
sum+=n%10;
n/=10;
}
System.out.println("Sum of digits &ldquo; +sum);

}
}
```

Output:
Enter a number
123
Sum of digits
6

With this, we have reached towards the end of this tutorial on “Sum of Digits in Java “. I hope It helped you in some way. Keep reading, keep exploring! We will keep digging the Java world together. Stay tuned!

Make sure you practice as much as possible and revert your experience.

This brings us to the end of our blog on “Java Program to find Sum of Digits in Java”.  I hope you found this blog informative and added value to your knowledge.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Sum of Digits in Java” article and we will get back to you as soon as possible. REGISTER FOR FREE WEBINAR Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month