Java program to calculate the sum of digits of a number. Here we will discuss the various methods to calculate the sum of the digits of any given number with the help of Java Programs. The compiler has been added so that you can execute the program yourself, alongside suitable examples and sample outputs. The methods as mentioned above are:
- Using For Loop.
- Using Static Method.
- Using Command Line Arguments.
- Using Recursion.
Using For Loop
1) Here we are using the for loop to calculate the sum of digits of a number.
2) Read the entered long value using scanner class object sc.nextLong().
3) The for loop iterates up to n!=0, here sum=0, and n=n/10,add the remainder of n/10 to the sum until n!=0 is false. If n=0 then for loop will be terminated, and prints the sum value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Scanner; 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 of a number is "+sum); } } |
Output:
1 2 3 |
Enter a number 12345678910 Sum of digits of a number is 46 |
Using Static Method
1) Static method sum(long num), will calculate the sum of digits of a number.
2) Read entered value. Call the static method sum(n) in the main method then the static method will be executed. This method adds the remainder to sum and n=n/10, these 2 steps will repeat until num!=0. If num=0 then it returns the sum value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; 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 of a number is "+s); } static int sum(long num) { int sum=0; while(num!=0) { sum+=num%10; num/=10; } return sum; } } |
Output:
1 2 3 |
Enter a number 5299 Sum of digits of a number is 25 |
Using Command Line Arguments
1) The command line arguments will be passed to “String args[]” of the main method.
2) Using Long.parseLong(arg[0]), we are converting the value at index 0 as long, here Long is the wrapper class.
3) Repeat these two steps sum=sum+n%10; n/=10; until n!=0, if n=0 then while terminated and print the sum value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 of a number is "+sum); } } |
Output:
1 2 3 |
>java SumOfDigits 9871 Enter a number Sum of digits of a number is 25 |
Using Recursion
1) We are calling the method using the class SumOfDigits to object “s” as s.sum(n) then sum() starts the execution.
2) Sum method calls itself as sum(num), it repeats until num!=0. If num=0 then it returns the sum value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 of a number is "+s.sum(n)); } } |
Output:
1 2 3 |
Enter a number 4012017 Sum of digits of a number is 15 |