Answers for "Write a function digitsum that calculates the digit sum of an integer. The digit sum of an integer is the sum of all its digits. Example: digitsum(192) should return 12."

1

To add all the digits of a number till you get a single digit.

#include<bits/stdc++.h> 
using namespace std; 
int digSum(int n) 
{ 
	if (n == 0) 
	return 0; 
	return (n % 9 == 0) ? 9 : (n % 9); 
} 
int main() 
{ 
	int n = 9999; 
	cout<<digSum(n); 
	return 0; 
}
Posted by: Guest on August-17-2020
0

Write a function digitsum that calculates the digit sum of an integer. The digit sum of an integer is the sum of all its digits.

function digSum(n) {
    let sum = 0;
    let str = n.toString();
    console.log(parseInt(str.substring(0, 1)));
    for (let i = 0; i < str.length; i++) {
        sum += parseInt(str.substring(i,i+1));
        
    }
    return sum;
}
Posted by: Guest on January-01-2021
0

Write a function digitsum that calculates the digit sum of an integer. The digit sum of an integer is the sum of all its digits. Example: digitsum(192) should return 12.

function digitsum(n){
 let sum = 0;
 n = n.toString();
 for(let i=0; i<n.length; i++) {
   sum += parseInt(n[i]);
 }
 return sum;
}
Posted by: Guest on September-22-2021
0

Given a long number, return all the possible sum of two digits of it. For example, 12345: all possible sum of two digits from that number are:

function digits(num){
  let numArray = num.toString().split('');
  let sumArray = [];
  
  for (let i = 0; i < numArray.length; i++) {
    for (let j = i+1; j < numArray.length; j++) {
      let sum;
      sum = Number(numArray[i]) + Number(numArray[j]);
      sumArray.push(sum);
    }
  } 
  return sumArray;  
}
Posted by: Guest on August-12-2020

Code answers related to "Write a function digitsum that calculates the digit sum of an integer. The digit sum of an integer is the sum of all its digits. Example: digitsum(192) should return 12."

Code answers related to "TypeScript"

Browse Popular Code Answers by Language