Question: 6.2 Using a method ( 10 points ): (Sum the digits in an integer) Write a java program SumDigByMethod.java that uses a method sumDigits(long number)

6.2 Using a method (10 points): (Sum the digits in an integer) Write a java program SumDigByMethod.java that uses a method sumDigits(long number) to calculate the sum of the individual digits in a user-input number (integer or long). The method sumDigits( ) returns an integer value. The user- input number can have up to 10 digits. Do not assume or force a specific number of digits. In your main( ) method, prompt the user to enter the number. Then make a call to sumDigits( ) and pass it on the number entered by the user. The method sumDigits( ) should then calculate the sum of digits and send it back to main( ). Displays the sum of digits from main( ) after main receives it from sumDigits( ). [While it is not mandatory, writing and using a numOfDigits( ) method to determine the number of digits in a number could help this one and the next program.]

Sample Run

This program uses a method sumDigits( ) to sum the digits in a number. Please enter the number: 1234 Printed from main( ): The number entered is: 1234

Making a call to sumDigits( ) Back in main( )

The sum of digits in the number 1234 is: 10

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!