tag:blogger.com,1999:blog-8712770457197348465.post2417982506133438467..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: Java Program to Find Sum of Digits in a Number using Recursion? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8712770457197348465.post-91665028588613703412020-11-04T00:30:08.666-08:002020-11-04T00:30:08.666-08:00Hello Ansul, do you hava testcase to expose that b...Hello Ansul, do you hava testcase to expose that bug? what about "1" ? 1/10 is also zero so I think the function is Ok as base case is passing 1? No?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84088293404823581462020-11-01T05:24:19.564-08:002020-11-01T05:24:19.564-08:00there is a bug in first function
public int getSu...there is a bug in first function<br /><br />public int getSumOfDigits(int num) {<br /> // here num is 0 , you dont need to do num/10 , its already coming as num/10<br /> if (num == 0) return 0;<br /> return getSumOfDigits(num / 10) + num % 10;<br />}Anshul Kattahttps://www.blogger.com/profile/06124455840498634217noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-34786130997605566452013-07-30T11:04:01.580-07:002013-07-30T11:04:01.580-07:00Sum of digits os the addition of the digits presen...Sum of digits os the addition of the digits present in the program.you can get the code from http://letusprogram.wordpress.com/2013/07/12/sum-of-digits-in-java/Anonymoushttps://www.blogger.com/profile/03429427227455008450noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84171337335786249632013-05-19T23:10:44.205-07:002013-05-19T23:10:44.205-07:00I may do it in this way, it's easier for me to...I may do it in this way, it's easier for me to understand,<br /> public static int sumOfDigits(int digit)<br /> {<br /> int re = 0;<br /> while(digit != 0)<br /> {<br /> re += digit%10;<br /> digit /= 10;<br /> }<br /> return re;<br /> }Anonymousnoreply@blogger.com