+8

# Can someone help me plz

How do I run a program where you can find the sum of all digits in an odd location of a number (the leftmost digit is location #1. Please help)

12/16/2018 11:25:05 PM

Michelle Saulsbury

+7

Here I've fixed some of it. Putting all variables and pseudocode from my post above. https://code.sololearn.com/c95Vk547kR4b You should continue putting code in like I did.

+7

I like to do it this way https://code.sololearn.com/cQDa7poi9Ayv/?ref=app

+5

Michelle Saulsbury BTW... There are several ways this could have been done. I just chose to use this approach because there were no restrictions and it was the most straightforward option that came to mind. If you were not permitted to use the strings type, then this could have been done using a char array. If you were required to work with an integer, then John Wells was leading you in the right direction. I just wanted to clarify that my approach is simply one of many options that could have been used.

+4

Moses Odhiambo I really like your approach. It's nice, straight forward, and very clean.

+3

+3

Gordon he has no challenges. He has lost xp by either using hints or wrong answers to the quizes.

+3

Another way of finding out the number of digits is by combining these : https://code.sololearn.com/c0X071FBZCSX/?ref=app https://code.sololearn.com/c3o3rkp5lB4w/?ref=app

+2

Copy the number. Zero the count. While the copy is not zero, ... Increment the count. Divide the copy by 10. Endwhile Copy the number once more. Zero the sum. While the copy is not zero, ... Modulus the copy by 10 for current digit (d = n%10). Divide the copy by 10 (n /= 10). If the count is odd (c%2 == 1), ... Add the digit to the sum. Endif Decrement the count. Endwhile

+2

thank you for the help

+2

I've taken a different approach but purposely, left it incomplete. I'm going Michelle Saulsbury might be able to see a path forward with completing the logic. https://code.sololearn.com/c58KqL0IlT57/?ref=app

+2

David Carroll leave it to you to find a simpler way to get the odd digits.

+2

I tend to have a blindspot to using a number as a string. I don't think that way because of the memory available back in the time I started.

+2

while (num>0) { t=num%100; num/=100; sl+=t/10; sr+=t%10; if (num>9) { sl+=t/10; sum=sl; } else { sr+=num; sum=sr; } } My only problem is knowing the number of digits in the original number so I know which digits are odd. The last if stmt should solve that problem.

+2

Mack Praise Please post your question in a new Question Post and remove these from this thread. Posting new questions in other people's threads is considered spamming.

+2

Have look here (integers only) https://code.sololearn.com/cz3We6c928wk/?ref=app

+2

https://code.sololearn.com/cRCSeKqFlS1n/?ref=app

+2

David Carroll If you are using strings then direct indexing is the most straight forward and cleanest way

+1

https://code.sololearn.com/ctD3EnOb5swA/?ref=app

+1

I'll keep helping until done, but bedtime is soon.

+1

LOL... I saw that the challenge mentioned starting from the left. So, using the mod operator would require some additional work since it reduces from the right. If summing from the right most digit, I would have taken your approach and used something like num % 100 to reduce by two digits.