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