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
0
votes
2 answers

Sum of positive integers - Prints Garbage value

The program must accept N integers and print the sum S of all POSITIVE integers with the even positive integers reversed. Example Input/Output 1: Input: 4 39 -8 57 24 Output: 138 Explanation: The sum = 39+57+42 = 138 (The even number 24 is…
0
votes
0 answers

Adding linked list together and printing the result

My problem is asking to have two linked lists where each node has one value and add them together into a different linked list. But, in order to get the digits of the list, I will have to do the modulo and division operator to shift my decimal and…
Zeid Tisnes
  • 396
  • 5
  • 22
0
votes
1 answer

Adding Digits in a Number (Need Code Explanation)

I came across this code segment elsewhere. It simply adds all the digits in a given number: def sumDigits(n): sum = 0 while n > 0: sum += n % 10 n //= 10 return sum Problem is, I don't get the logic behind it at all. In…
Fiery Phoenix
  • 1,156
  • 2
  • 16
  • 30
0
votes
1 answer

Crystal Reports Sequence for Fields

I'm pretty new on using Crystal Reports. So, in the picture below: I need the first value in the table (1,041,410.00, 1,300,084.00) to add themselves up for every new date in the table. For example: In the third row of 12/30/2011, the values are…
0
votes
2 answers

Finite-state automation that accepts sum of digits divisible by n

Finite state machine that accepts if sum of digits divisible by 3 . I am trying to construct a finite state machine the accepts if the sum of digits is divisible by n. So far I was able to do for n=2 and n=3 but dint find any generalized steps that…
0
votes
1 answer

why my code doesn't work when my input contain character? I want my code to ignore character

package code; public class Solution3 { public static int sumOfDigit(String s) { int total = 0; for(int i = 0; i < s.length(); i++) { total = total + Integer.parseInt(s.substring(i,i+1)); } return…
users1141df
  • 19
  • 1
  • 6
0
votes
2 answers

Python sum of digits error

While solving Project Euler problems with Python (which I am a beginner at), I got this following error. The question is to find the sum of the digits of 2^1000. For that I wrote the following code : sum=0 x=2**1000 while(x): sum += x%10 …
user3303356
0
votes
3 answers

Sum of the series. What's wrong with this Python code?

I'm trying to write a program to find a sum of all multiples of 3 below given n. So far I came up with this solution (using formula for arithmetic series): def sumof3(n): n = int((n-1)/3) return int((n/2)*(6+3*(n-1))) It works pretty well…
0
votes
1 answer

Sum of the Digits program error

I have been working on a program to take integer x and find any numbers whose sum of the digits multiplied by x equals the number. my code has worked for numbers 2, 3, and 4, but beyond that is returns various errors regardless of what i am doing.…
0
votes
1 answer

Sum of digits issue

I'm doing some problems of Project Euler and I've stumbled upon an issue. I have no idea why this algorithm doesn't work for 2^1000. It works for numbers in the range of 10^1 and 10^8 (Those are the ones I tested), but it should work for every…
Slyforce
  • 5
  • 4
0
votes
1 answer

Sum of number using user entered 4 digit numbers

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment_1 { class Program { static void Main(string[] args) { double…
Shahze123
  • 43
  • 1
  • 10
0
votes
3 answers

My 'cout' isn't giving correct value - why?

Simple "sum of digits" code. It compiles but when executed, the last cout gives a "0" for the num int rather than the actual user-input number. Feel free to copy and paste this into your own compiler, if you're so inclined, to see what I mean. How…
Codeur
-1
votes
2 answers

Finding a number that has the same number of digits as a given number and is smaller and has the same the sum of digit python

Find a number, which is smaller than a given number, and has the same amounts of digit as it, and the sum of digits is the same. N = int(input()) sume = 0 lik = [int(x) for x in str(N)] for k in range(len(lik)): sume += lik[k] def get_sum(N): …
Codeer
  • 82
  • 9
-1
votes
1 answer

How to find sum of these values in Pandas?

I'm trying to create a script for analysing my datas. Here is my problem. Let's say I have an excel file like that. Code Value A1 20 B1 30 A1 15 C1 20 B1 20 I need Pandas to do this file like this. And write an excel file. I got an…
Safak
  • 1
-1
votes
1 answer

Sum of digit of a number using recursion in python

Here in this program, I tried to understand but couldn't get completely. How is this recursive function doing the sum and returning total sum of this? Please explain me in detail? # Recursive Python3 program to # find sum of digits of a number #…