+ 1

Cprogram-add all num using recursion:67=6+7=output=1+3=4. The code output is correct!! this recursion method is correct or not??

#include <stdio.h> int jansi(int a) { if((a/10)!=0) { return (a%10)+jansi(a/10); } else { return a; } } int main() { int a,value; scanf("%d",&a); value=jansi(a); while( value/10!=0) { value=jansi(value); } printf("%d",value); return 0; }

20th May 2020, 7:33 PM
T.Jansi Rani
T.Jansi Rani - avatar
6 odpowiedzi
0
Until become single digit number.. Yes thats what you trying.. Is not it..? 67=> 6+7=13=>1+3=4.
21st May 2020, 8:27 PM
Jayakrishna 🇮🇳
+ 1
You can check in the playground by saving code and running it..? Do you have any difficulty in that..? Just try to remove warning....
20th May 2020, 7:43 PM
Jayakrishna 🇮🇳
+ 1
Ok thank you 🔥🔥
21st May 2020, 8:17 PM
T.Jansi Rani
T.Jansi Rani - avatar
+ 1
The code output is correct!! But,this recursion method is correct or not?? because,calling the function is continuing till the value becomes 1 digit number!! Check and tell me!!
21st May 2020, 8:22 PM
T.Jansi Rani
T.Jansi Rani - avatar
+ 1
Yes
21st May 2020, 8:28 PM
T.Jansi Rani
T.Jansi Rani - avatar
0
Yes, it works well. Just remove the two unused variables b and sum in jansi() in order to be compilable in SoloLearn.
20th May 2020, 8:59 PM
Bilbo Baggins
Bilbo Baggins - avatar