Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java Program for Sum of the digits of a given number

// Java program to compute
// sum of digits in number.
import java.io.*;
 
class GFG {
 
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 687;
 
        System.out.println(getSum(n));
    }
}
 
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #java #Program #Sum #digits #number
ADD COMMENT
Topic
Name
7+7 =