Sum of Digits C-String help.

So i need to add a sum of digits from 1 to 1000. for example: if u add the digits in 468 you get 4+6+8 = 18 and the squared and cube of the digit = 18 also. i have to output a list of positive integers (k,k2,k3) to add up to same number. I have written down in pseudocode but i need help with making it into an actual program.




//for n = 1 to 1000 {
// nsquared = n * n;
// ncubed = n * n * n;
// convert n to string array (atoi)
// convert nsquared to string array (atoi)
// convert ncubed to sting array (atoi)

// sumofdigits = 0;
// for each digit in n's array {
// copy digit to array of size 2 with second character being '\0'
// convert digit array to integer
// add integer to sumofdigits

theres another separate loop for nsquared and ncubed


//if(sumofdigits == sumofnsdigits == sumofncdigits) {
// print n
First of all, there is no need to convert to a string. Second you could get the digits of a number with the / and % operators.

In case anyone else was confused:
468: 4+6+8 = 18
4682= 219024: 2+1+9+0+2+4=18
4683= 102503232: 1+0+2+5+3+2+3+2=18

Also, make sure you use long long.

Edit: Another thing, you should have a 'digisum' function instead of
another separate loop for nsquared and ncubed
Last edited on
Topic archived. No new replies allowed.