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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!