Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this hw problem: Using recursion, create a program that will allow for a user to enter 5 numbers. The program will provide the

I have this hw problem: Using recursion, create a program that will allow for a user to enter 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: * PLEASE NOTE, I WANT TO KEEP THE CODE RELATIVELY THE SAME (HOW I SOLVE THE PROBLEM) AND IF THERE ARE ANY CHANGES MADE, I WOULD LIKE TO KNOW WHY THOSE CHANGES ARE BEST*

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions

Question

=+ a. How does this change affect the incentives for working?

Answered: 1 week ago