Questions tagged [sum-of-digits]

A sum of digits, or digital sum, is a number created by taking each digit of one or more other numbers and adding them together.

For example, the sum of digits for the number 12,345 would be 15 because:

1 + 2 + 3 + 4 + 5 = 15

The sum of digits for the numbers 12,345 and 67,890 together would be 45 because:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 = 45

Note that a sum of digits will always be an integer.

60 questions
-1
votes
4 answers

How do I do sum_of_digits recursively for large numbers

Currently, I have written a recursive function to do sum_of_digits but it works for smaller numbers, e.g. Less than 6. But for larger numbers, the calculation messes up for some reason. def sum_of_digits(i): if i == 0 : return i …
kraljevocs
  • 115
  • 1
  • 2
  • 12
-1
votes
1 answer

sum of numbers/extact digits

I am new to coding and try to extract and print the none digits. I've written 2 different codes but cannot combine them. I would appreciate some advices. (i tried using *args but didn't work) def SumOfDigits(str1): sum_digit = 0 for x in…
El. Nik
  • 11
  • 1
-1
votes
2 answers

Making sum of two variable with extra text X=

Two variables A and B. I need to print sum of these two variables with extra word X= like, input A=3; B=2; output, X=5; I need (X=5;) this total answer with X= how can I do it in javascript?? please help me.
-1
votes
1 answer

finding the sum of digits?

I tried this problem using for loop but i did not got the correct output as specified . i do not know what is the problem in my code ? #include int main() { int T,N; scanf("%d %d" ,&T,&N); for(int i=N;i>0;i=i/10) …
-1
votes
3 answers

How to calculate sum of all input value in text having trailing commas?

I want to calculate all input values in text which are having or not having trailing commas. Example I input 2,3,4,5 or 2345 in text box. Its sum should come as 14, provided user has used or not used , in between numbers.
-1
votes
3 answers

How to find sum of integers in a string using JavaScript

I created a function with a regular expression and then iterated over the array by adding the previous total to the next index in the array. My code isn't working. Is my logic off? Ignore the syntax function sumofArr(arr) { // here i create a…
Phoenox7
  • 1
  • 1
  • 1
-1
votes
3 answers

Unknown factor in answer

I can't find the problem with what I wrote. I am attempting Project Euler #16, where I need to sum all digits of 2^1000. My program works with small numbers, but as numbers get around 18 digits or so, it breaks. Any help? public static double…
Brian
  • 167
  • 1
  • 9
-1
votes
4 answers

a simple program that counts and sums digits. How can I make it work?

So I've to write a simple program(that loops) where you can enter an int and it spews out the number count and the sum of the numbers. Since I am such a tard when it comes to programming, I just scavenged the code online and tried to piece it…
user1710386
  • 23
  • 1
  • 1
  • 2
-2
votes
2 answers

Sum of digits in C++

program asked is sum of digits : Input data are in the following format: first line contains N - the number of values to process; and then N lines will follow describing the values for which sum of digits should be calculated by 3 integers A B…
-2
votes
1 answer

Sum of digits of non-negative number using list comprehension

I'm looking for a non-recursive implementation of sum of digits (a "cross sum") of a non-negative number like this: cs :: Int -> Int cs n = sum digits_of_n where digits_of_n = [ . | ... ] Basically: How does one get a list of digits from a…
-2
votes
1 answer

C++ Program to print sum of digits

//Program to print sum of digits #include using namespace std; int main() { int n, m, sum = 0; cin >> n; for(int i = 0; i < n; i++) { m = n % 10; sum += m; n = n / 10; …
-2
votes
2 answers

Calculate the sum of the digits using python

I would like to find the sum of the digits using python. when i enter a birth year 1982 the result should be 1+9+8+2 = 20 final total result is 2+0 = 2. The reason that i am posting this question is i didn't find any simple python solution for…
Arun Oid
  • 11
  • 6
-3
votes
3 answers

Getting the summation of the 1,2 and 3 digits numbers from text file in perl

Suppose a set of numbers is given in a file number_list.txt. Find the sum of the numbers from the file in the following categories: Sum of all 1 digits numbers Sum of all 2 digits numbers Sum of all 3 digits numbers Sum of all numbers starting…
-5
votes
1 answer

why does my code get segment fault on SPOJ for Sum of Digits problem?

When I submitted the solution of a dp problem on spoj for this problem I always get a segment fault. But my solution works on other platforms like visual studio and Ideone. I do not know why I am getting this error, Can you help? My code: #include…
Aya Ahmed
  • 3
  • 2
-7
votes
3 answers

Sum of long integer array in c++

I know this question asked many times but I am facing different problem in my code, I try to calculate sum of long integers range between 2-15. Code: long array[20]; long NUMBERS; cout << "How many numbers ? : "; cin >> NUMBERS; long sum=0; for…
Omore
  • 614
  • 6
  • 18
1 2 3
4