Computer Science, asked by ShanayaTrivedi, 1 year ago


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 syedmdsaif827
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

syedmdsaif827: űă most welcome
ShanayaTrivedi: Thanks a lot for your help!!
syedmdsaif827: it's correct or wrong
ShanayaTrivedi: everything's correct except that....i think the n value was supposed to be accepted at execution.
syedmdsaif827: hmm
syedmdsaif827: mark it as brainlist plZ
Answered by Anonymous
10

Hi

function calcGivenNumbers (nums) {

 var numsArr = ("" + nums).split("");

 var total = 0;

 numsArr.forEach(num => {

   total += parseInt(num);

 });

 

 document.write(total);

}

calcGivenNumbers(3987);

Attachments:

ShanayaTrivedi: Thanks a lot for helping me
Similar questions