Find the sum of the digits of an integer that is input. Sample Input: 15390 Sample Output: Sum of the digits: 18
Please help me with a solved program!!
Answers
Answered by
15
heya here is ua answer.
.........................
// 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 program
public static void main(String[] args)
{
int n = 687;
System.out.println(getSum(n));
}
}
// This code is contributed by Gi
if it is correct plz mark it as brainlist
.........................
// 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 program
public static void main(String[] args)
{
int n = 687;
System.out.println(getSum(n));
}
}
// This code is contributed by Gi
if it is correct plz mark it as brainlist
syedmdsaif827:
űă most welcome
Answered by
10
Hi
function calcGivenNumbers (nums) {
var numsArr = ("" + nums).split("");
var total = 0;
numsArr.forEach(num => {
total += parseInt(num);
});
document.write(total);
}
calcGivenNumbers(3987);
Attachments:
Similar questions