You are given a large integer N. Find the sum of its digits. Eg:- if the integer is 1234, the answer is 1+2+3+4=10. code example
Example 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;
}
Example 2: 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;
}