How to find the Sum of Digits in Java?

Last updated on Jun 17,2021 16.7K Views

How to find the Sum of Digits in Java?

edureka.co

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[0]);
while(n!=0)
{
sum+=n%10;
n/=10;
}
System.out.println("Sum of digits “ +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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial