0

Can u please help me to do it??

Hi friends , I have been solving a problem , the aim is to calculate sum of digits of a number , and this algorithm must run as long as the output has only one digit Example 1 : 53 , output :8 . Example 2 : 67 , output : 4 . (6+7=13,1+3=4) Actually i know how to make a program for the first example , but what if sum of digits is more than 9 ?? Plz help me thanks.

20th Feb 2021, 5:24 AM
Ali_combination
Ali_combination - avatar
13 ответов
+ 2
I will not add a do..while loop for you: I gave you the tip, do the (easy) implementation yourself ^^
20th Feb 2021, 5:42 AM
visph
visph - avatar
+ 1
Can you post your code link here if possible for the first case? edited: Solution 1(Ali_combination your solution): #include <bits/stdc++.h> using namespace std; int main() { int n=12345; int a=ceil(log10(n)); int sum=0; do{ sum = 0; for(int i=1; i<=a; i++){ int p=pow(10,i-1); int ab=(n/p)%10; sum=sum+ab; } n = sum; cout<<sum<<endl; }while(sum>9); return 0; } Solution 2(using string): #include <iostream> using namespace std; int main() { string a = "12345"; int _sum = 0; do{ _sum = 0; for(unsigned i = 0; i<a.length(); i++){ _sum += a[i]-'0'; } a = to_string(_sum); cout << a << endl; }while(_sum>9); return 0; }
20th Feb 2021, 5:31 AM
minirkk
minirkk - avatar
20th Feb 2021, 5:56 AM
Indranil
Indranil - avatar
+ 1
Indranil Bhadra thank you bro😊
20th Feb 2021, 5:59 AM
Ali_combination
Ali_combination - avatar
0
while 9 < result, compute digits sum...
20th Feb 2021, 5:35 AM
visph
visph - avatar
20th Feb 2021, 5:38 AM
Ali_combination
Ali_combination - avatar
0
if we do it for you, you could be sure you don't learn anything ;P
20th Feb 2021, 5:47 AM
visph
visph - avatar
0
That's actually a precious true fact...s i'll try to do it..thanks.
20th Feb 2021, 5:51 AM
Ali_combination
Ali_combination - avatar
0
RKK i really appeciate you for your help..😊 .. that should be a great solution but now I want to try to pull it off for the last time..hope i'll make it , otherwise I'll check out your code .
20th Feb 2021, 5:56 AM
Ali_combination
Ali_combination - avatar
20th Feb 2021, 5:59 AM
Indranil
Indranil - avatar
- 1
visph that's true , but i haven't made it yet ... can you please do it ? I'll appreciate you🙌
20th Feb 2021, 5:39 AM
Ali_combination
Ali_combination - avatar
- 1
visph alright 😄 I'll try to do that for the thousand time...but please believe it's been almost 2 weeks that Iam trying to make the program but still i got nothing🤦‍♂️
20th Feb 2021, 5:45 AM
Ali_combination
Ali_combination - avatar
- 3
Rearrange valid javascript code for no variable out put console Pls sayyy
21st Feb 2021, 12:21 PM
vedururi tharun
vedururi tharun - avatar