+ 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)
31 odpowiedzi
+ 8
I like to do it this way
https://code.sololearn.com/cQDa7poi9Ayv/?ref=app
+ 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.
+ 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
Put it in a playground file. Use plus sign in circle icon to link it here.
+ 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
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.
+ 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.
+ 1
num=(n/=10);will raise Exception because there is no n declared.
num = num / 10; is what you were trying to do.
Shorthanded, is
num /= 10;
+ 1
I checked your profile, 5xp is low at your C++ course progress. When someone challenge you, you can choose decline.
For this problem, let me try to code a framework for you, based on your latest progress.
+ 1
Michelle Saulsbury Another way to try. You could break the integer value into bit values first.
Check out the function here
https://code.sololearn.com/cCVs8S543rMX/?ref=app
Once you have all the bit values, you could easily manipulate the values whatever you wish to.