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 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

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_2

Step: 3

blur-text-image_3

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

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions