Can someone help me plz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)

16th Dec 2018, 11:25 PM
Michelle Saulsbury
29 Answers
18th Dec 2018, 6:08 PM
Moses Odhiambo
Moses Odhiambo - avatar
+ 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.
17th Dec 2018, 3:23 AM
John Wells
John Wells - avatar
+ 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.
17th Dec 2018, 4:22 AM
David Carroll
David Carroll - avatar
+ 4
Moses Odhiambo I really like your approach. It's nice, straight forward, and very clean.
18th Dec 2018, 6:55 PM
David Carroll
David Carroll - avatar
+ 3
Put it in a playground file. Use plus sign in circle icon to link it here.
17th Dec 2018, 1:34 AM
John Wells
John Wells - avatar
+ 3
Gordon he has no challenges. He has lost xp by either using hints or wrong answers to the quizes.
17th Dec 2018, 4:16 AM
John Wells
John Wells - avatar
+ 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
17th Dec 2018, 4:48 AM
Gordon
Gordon - avatar
+ 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
17th Dec 2018, 12:38 AM
John Wells
John Wells - avatar
+ 2
thank you for the help
17th Dec 2018, 3:25 AM
Michelle Saulsbury
+ 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
17th Dec 2018, 3:54 AM
David Carroll
David Carroll - avatar
+ 2
David Carroll leave it to you to find a simpler way to get the odd digits.
17th Dec 2018, 3:57 AM
John Wells
John Wells - avatar
+ 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.
17th Dec 2018, 4:05 AM
John Wells
John Wells - avatar
+ 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.
18th Dec 2018, 1:48 PM
David Carroll
David Carroll - avatar
18th Dec 2018, 7:17 PM
Michelle Saulsbury
+ 1
https://code.sololearn.com/ctD3EnOb5swA/?ref=app
17th Dec 2018, 1:42 AM
Michelle Saulsbury
+ 1
I'll keep helping until done, but bedtime is soon.
17th Dec 2018, 3:27 AM
John Wells
John Wells - avatar
+ 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.
17th Dec 2018, 4:01 AM
David Carroll
David Carroll - avatar
+ 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;
17th Dec 2018, 4:07 AM
Gordon
Gordon - avatar
+ 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.
17th Dec 2018, 4:13 AM
Gordon
Gordon - avatar
+ 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.
17th Dec 2018, 5:07 AM
Calviղ
Calviղ - avatar