Question

1. (Sum the digits in an integer) Write a method that computes the sum of the...

1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header:

public static int sumDigits(long n)

For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23). Use a loop to repeatedly extract and remove the digit until all the digits are extracted. Write a test program that prompts the user to enter an integer and displays the sum of all its digits.

Sample run in console:

Enter a number:1982222

The sum of the digits for 1982222 is 26

Then in a different class (Display an integer reversed) Write a method with the following header to display an integer in reverse order.

  public static void reverse(int number)

For example, reverse (3456) displays 6543. Write a test program that prompts the user to enter an integer and displays its reversal.

sample run in console

enter an integer: 1231982

2891321

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code for both functions:-

//importing the library
import java.io.*;
import java.util.*;

//Class having function to Sum the digits of numbers   
class SumNumber{
  
/* Function to get sum of digits */
public static void sumDigits(long n) {   
  
long sum = 0,k=n;
  
while (n != 0){
  
sum=sum+n%10;
n = n/10;
}
  
//printing the output
System.out.println("The sum of the digits for " + k + " is "+sum);
}   
  
}

//Class having function to print the reverse of givrn number
class ReverseNumber{

/* Function to print the reverse of givrn number */
public static void reverse(int number)
{
//reversed varibale used to store the reverse number
long reversed = 0;

while(number != 0)
{
long digit =number % 10;
reversed = reversed * 10 + digit;
number/= 10;
}
  
//printing the output
System.out.println(reversed);
}
}

public class printdata
{
public static void main(String[] args)
{
//scanner for scanning the value
Scanner sc=new Scanner(System.in);
  
//Enter the digit for sum of digit operation
System.out.println("enter an integer:");
long l=sc.nextInt();
//since SumNumber class have static functon we are not worried about defining the object of that class
SumNumber.sumDigits(l);
  
//Enter the digit for reversing the number
System.out.println("enter an integer:");
int n=sc.nextInt();
//since ReverseNumber class have static functon we are not worried about defining the object of that class
ReverseNumber.reverse(n);
  
}
}

Add a comment
Know the answer?
Add Answer to:
1. (Sum the digits in an integer) Write a method that computes the sum of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

    PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27

  • Code in C An integer is divisible by 9 if the sum of its digits is...

    Code in C An integer is divisible by 9 if the sum of its digits is divisibleExample output: by 9. Develop a program which will call UDF: int get input(); to prompt the user for an integer and return this user input to main() Call UDF: Enter an integer: 5463 3 4 5 5463 is divisible by 9 void display int val); to display each digit of the integer starting with the rightmost digit. Your program should also determine whether...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

  • Program : Write a complete C++ program that Input: Read a positive integer from the keyboard...

    Program : Write a complete C++ program that Input: Read a positive integer from the keyboard (user) with proper prompt text and save it to a variable. The integer have up to five digits. Processing: From the right most digit, extract every digit from the input integer using modular operator %then save the digit to a separate variable. Remove the right most digit from the integer using integer division operator /. Repeat above two steps till all digits have been...

  • Write a function that returns the number of digits in an integer using the following header:...

    Write a function that returns the number of digits in an integer using the following header: int getSize(int n) For example, getSize(45) returns 2, getSize(3434) returns 4, getSize(4) returns 1, and getSize(0) returns 1. Write a test program that prompts the user to enter an integer and displays its size.

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • Please use public class for java. Thank you. 1. (10 points) Write a method that computes...

    Please use public class for java. Thank you. 1. (10 points) Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula futurelnvestmentValue numberOfYears 12 investmentAmount X ( monthlyInterestRate) Use the following method header: public static double futurelnvestmentValue( double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue 10000, 0.05/12, 5) returns 12833.59. Write a test program that prompts the user to enter the investment...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT