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
vote
4 answers

Append a digit to an integer and make sure sum of each digits ends with 1

What is the algorithm in c# to do this? Example 1: Given n = 972, function will then append 3 to make 9723, because 9 + 7 + 2 + 3 = 21 (ends with 1). Function should return 3. Example 2: Given n = 33, function will then append 5 to make 335,…
Mint
  • 37
  • 5
1
vote
6 answers

Understanding methods. Java code

Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) Programming problem 5.2. Page 212. Please forgive my newness to programming. I'm having a hard time…
Pivo
  • 31
  • 1
  • 10
1
vote
2 answers

Sum of Digits using recursion in C

For our activity today, we were tasked to make using recursion with the sum of digits. I already made this program: int main() { int num = 0, sum; printf("Enter an integer: "); scanf("%d",&num); //counter=1; for ( sum=0; num>0;) { sum = sum…
JustAStudent
  • 99
  • 1
  • 5
  • 10
0
votes
0 answers

How to find all possible sums of all digits of integer?

I need to find all possible sums of all digits of integer. Input: 234 Output: 36 Explanation: (2+3+2+4)+(3+2+3+4)+(4+2+4+3) But I only know how to find the sum of the firs and last digit: int num, sum = 0, firstDigit,…
0
votes
0 answers

sum-of-digits issue with program

public class quersumme { public static int quersum(int a) { for(int summe=0;) {while (a>0) {summe=summe+a%10;} } return a; } public static void main(String[] args) { int Wert=123; System.out.print(quersum(Wert)); } } Program has issues with the…
0
votes
1 answer

Function for digitsum of all n digit numbers and stored in sum but not getting the correct ans

This the the function that store the value in sum for digitsum of all n digit number void findsum(int pos,int n,int& sum) { if(pos>n){ return; } for (int i = 0; i < 10; i++) { findsum(pos+1,n,sum); sum+=i; …
0
votes
3 answers

Cross sum of digits of a 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 = [ . | ... ] A cross sum of a number (e.g. 512) is the sum of its…
0
votes
2 answers

Can I compute digital sum of 2ⁿ more efficiently?

I am trying to make a recursive function in C that calculates the sum of digits in 2ⁿ, where n < 10⁷. I made something that works, but it's very slow (for n = 10⁵ it takes 19 seconds). The function must return the sum in maximum 1 second. My…
0
votes
1 answer

Additive Persistence and Additive Root Program Not Responding After Inputting Number

I have a homework assignment for my beginner's C programming class: Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed on the sum, repeating until…
obdaniels
  • 3
  • 2
0
votes
1 answer

function stack overflow when 5000+ deep recursion

This function works fine with smaller arrays. But, when, given very large array of "int"s it fails miserably. I looked up and found out its the stack that is out of memory which was causing the problem because it can't allocate enough space to hold…
user4283875
0
votes
1 answer

How to calculate the sum of digits in a number iteratively - java/bluej

I am creating two methods - one that calculates the sum of the digits in a number recursively, and the other iteratively. I have created the recursive method, and for the most part I understand the concept of finding the sum of digits, but I am not…
BirdMusic
  • 1
  • 3
0
votes
2 answers

64 Bit digitsum (C) [Feel Free to edit my English]

This is part of my assignment, but I don't know why the output is not correct. Help? /** * Create a function called digitsum that takes an long integer (64 bits) and * sums the value of each of its digits. i.e. the number 316…
Mike ODell
  • 37
  • 9
0
votes
2 answers

Get all n-digit numbers whose sum of digits equals to given sum

How can I get all n-digit numbers whose sum of digits equals to given sum? I need the fastest solution because n can be equal with 9 and sum can be equal with 1000. I have implemented the solution below but it's too slow... List l = new…
0
votes
0 answers

Summation of series 1/2 -3/4 5/6 -7/8......n

#include int main() { int n=1,num; float x=0,m; printf("Enter no. of digits:\n"); scanf("%d",&num); for(n=1;n<=num;n++) { m=(float)(2*n-1)/2*n; if(n%2==0) { m=m*(-1); } x+=m; } printf("Summation…
Web King
  • 19
  • 6
0
votes
2 answers

whats wrong with this while loop in unix

I am a newbie .Please help me with this The output says syntax error near unexpected token 'do' The code is if [ $# -eq 0 ]; then echo "Command line arguments are missing." else n=$1 sum=0 while[ $n -gt 0 ] do rem=$(( $n % 10 )) sum=$(( $sum…
Ravi Kd
  • 39
  • 8