Question

In: Computer Science

JAVA PLEASE Write a recursive function that does the following: Given a number, add all the...

JAVA PLEASE
Write a recursive function that does the following:
Given a number, add all the digits and display the sum.
Example:
​​The sum of the number 5432 would be 14.
o Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from the tail to the beginning.
o The input is going to be received as a single integer from the user, in the main function. The input will not be more than four digits long (no validation necessary).

Solutions

Expert Solution

//T estDigitSum.java

import java.util.Scanner;
public class TestDigitSum{
   //recursive method for sum of digits in num
   public int digitSum(int num){
       int sum=0;
       sum = num % 10;
       if(num == 0)
           return 0;
       else
           return sum + digitSum(num / 10);
   }
   //main method
   public static void main(String[] args){
       int count=0,num;
       Scanner read = new Scanner(System.in);   //for reading inputs
       System.out.print("number for sum of digits:");
       num = read.nextInt();   //reading number from user
       //object creation
       TestDigitSum ds = new TestDigitSum();
       int dSum = ds.digitSum(num);   //storing medthod return value
       System.out.println("Digit Sum:"+dSum);   //displaying result
   }
}


Related Solutions

Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
Problem 1: Recursive anagram finder please used Java please Write a program that will take a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a word and print out every combination of letters in that word. For example, "sam" would print out sam, sma, asm, ams, mas, msa (the ordering doesn't matter) input: cram output: cram crma carm camr cmra cmar rcam rcma racm ramc rmca rmac acrm acmr arcm armc amcr amrc mcra mcar mrca mrac macr marc Hints: Your recursive function should have no return value and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT