Sum of digits of a number in Java
In this post, you will learn how to calculate the sum of digits of a number in Java. Such a type of question is generally ask in programming interview. There are different ways to find the sum of digits of a number in Java. Here, we have mentioned most of them -
Sum of digits of a number using for loop
In the given example, we have created an instance of the scanner class and declared a variable to store the number. Then, we have asked the user to provide the number and used the for loop to calculate the sum of digits of a number.
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String arg[])
{
long num, sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
// Get long number input from the user
num=sc.nextLong();
for(sum=0; num!=0; num=num/10)
{
sum = sum + num % 10;
}
System.out.println("Sum of digits: "+sum);
}
}
Output of the above code:
Enter a number: 23443
Sum of digits: 16
Sum of digits using while loop
In the given example, we have asked the user to enter a number, then used the while loop to calculate the sum of digits.
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String args[])
{
// initializing variables
int num, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
//reads an integer from the user
num = sc.nextInt();
while(num > 0)
{
// the last digit of the given number
digit = num % 10;
//adds last digit to the variable sum
sum = sum + digit;
//removes the last digit from the number
num = num / 10;
}
//display the result
System.out.println("Sum of Digits: "+sum);
}
}
Output of the above code:
Enter the number: 877544
Sum of Digits: 35
Sum of digits using user-defined function
In the given example, we have defined a function to calculate the sum of digits of a number entered by the user. First, we ask the user to initialize the number and then find all the digits and calculate their sum by calling the function findSum().
import java.util.Scanner;
public class SumOfDigits
{
// define function to find the sum of digits of a number
static int findSum(int number)
{
// initializing variable to store the sum
int sum = 0;
while (number != 0)
{
// get the last digit from the number
// and add it to the variable sum
sum = sum + number % 10;
//removes the last digit
number = number/10;
}
return sum;
}
//driver code
public static void main(String args[])
{
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
//reads an integer from the user
num = sc.nextInt();
//calling the user-defined method and prints the result
System.out.println("The sum of digits: "+findSum(num));
}
}
Output of the above code:
Enter a number: 436578
The sum of digits: 33
Sum of digits of a number using recursion
Recursion function is a function which is called by itself. In the given example, we call the recursion function to find the sum of digits of a given number. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
public class SumOfDigits
{
// defining function
// to calculate the sum of digits of a number
static int findSum(int num)
{
return num == 0 ? 0 : num % 10 +findSum(num/10) ;
}
//driver code
public static void main(String args[])
{
int number = 345237;
System.out.println("The sum of digits: "+findSum(number));
}
}
Output of the above code:
The sum of digits: 24
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java