Question
I have this hw problem: Using recursion, create a program that will allow for a suer to eneter 5 numbers. The program will provide the
I have this hw problem: Using recursion, create a program that will allow for a suer to eneter 5 numbers. The program will provide the sum of all 5 numbers using recursive methods. I put together the following code. Please let me know what changes I should make and why:
import java.util.Scanner;
public class NumberSum {
public static int sumOfNumbers(int number) {
if (number == 0) {return 0};
else {return (number % 10 + (sumOfNumbers(number/10)));
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print(Enter a five-digit number: );
int num = scnr.nextInt();
int sum = sumOfNumbers(num);
System.out.println(Sum of the numbers entered is + sum);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started