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.
13 RĂ©ponses
+ 2
I will not add a do..while loop for you: I gave you the tip, do the (easy) implementation yourself ^^
+ 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;
}
+ 1
Hope it helps
https://code.sololearn.com/cQG0JRyPycdE/?ref=app
+ 1
Indranil Bhadra thank you brođ
0
while 9 < result, compute digits sum...
0
RKK here you are
https://code.sololearn.com/cyc9WK22z1ul/?ref=app
0
if we do it for you, you could be sure you don't learn anything ;P
0
That's actually a precious true fact...s i'll try to do it..thanks.
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 .
0
Ali_combination welcome
- 1
visph that's true , but i haven't made it yet ... can you please do it ? I'll appreciate youđ
- 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đ€Šââïž
- 3
Rearrange valid javascript code for no variable out put console
Pls sayyy