The Artima Developer Community
Sponsored Link

Java Buzz Forum
Sum of Digits using Recursion in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
Sum of Digits using Recursion in Java Posted: Jan 13, 2015 8:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: Sum of Digits using Recursion in Java
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
This is the second part of our article to solve this coding interview question,  how to find sum of digits of a integer number in Java. In first part, we have solved this problem without using recursion i.e. by using while loop and in this part we will solve it by using recursion. It's good to know different approaches to solve the same problem, this will help you to do well on coding interviews. While finding recursive algorithm, always search for base case, which requires special handling. Once you find the base case, you can easily code the method by delegating rest of processing to method itself, i.e. by using recursion.  In this problem, base case is when the number becomes zero, at that time our program is complete and we return the sum of digits of given number. Another property of a recursive algorithm is that with each passing steps your program approaches to result and problems becomes shorter. For example in this coding problem, after each call one digit from the number is reduced. So if you provide 5 digit number then it will require five steps to complete. One key thing you need to know to solve this problem is the trick to get last digit of an integral number. You can use modulo operator to do that, number%10 always return the last digit.
Read more »

Read: Sum of Digits using Recursion in Java

Topic: Web and Mobile Applications with Spring and Groovy Previous Topic   Next Topic Topic: Working with GZIP and compressed data

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use