1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ivanshal [37]
4 years ago
15

For example, sumDigits(234) returns 9 (= 2 + 3 + 4). (Hint: Use the % operator to extract digits and the / operator to remove th

e extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4 ). To remove 4 from 234, use 234 / 10 (= 2 3 ). 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 then displays the sum of all its digits. So in your main method you'd call sumDigits() like this and print what it returns. For example: System.out.println("Sum of the digits in the number 1234 is: " + sumDigits(1234)); Please watch this video on how to export your Java project from Eclipse:
Computers and Technology
1 answer:
madam [21]4 years ago
7 0

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

       System.out.print("Enter an integer: ");

       int number = input.nextInt();

 System.out.println("Sum of the digits in the number " + number + " is: " + sumDigits(number));

}

public static int sumDigits(int number){

       int remainder = (int)Math.abs(number);

       int sum = 0;

       

       while(remainder != 0){

           sum += (remainder % 10);

           remainder = remainder / 10;

       }

       return sum;

   }    

}

Explanation:

- Initialize two variables to hold <em>remainder</em> and <em>sum</em> values

- Initialize a while loop operates while the <em>remainder</em> is not equal to zero

- <u>Inside the loop</u>, extract digits and assign these digits into <em>sum</em>. Remove extracted digits from the <em>remainder</em>

- When all the digits are extracted, return the <em>sum</em> value

- Inside main, get input from the user and call the method

You might be interested in
Image-editing software is used to _____.
bulgar [2K]

Answer:

B)enhance a photograph

3 0
3 years ago
Read 2 more answers
Websites can have good content for a particular set of search phrases, yet rank poorly. Issues that may cause this to occur incl
raketka [301]

Answer:

B. deep organizational structure

Explanation:

A website with a deep organizational structure is a positive attribute that improves site performance.

5 0
3 years ago
What is meaning of reboot
TEA [102]
I believe to reboot is to reset. Its the act of resetting, or starting up a computer again.
4 0
4 years ago
Read 2 more answers
Plz plz plz plz plz plz plz plz plz plz plz pl plz plz plz plz plz plz plz plz plz plz plz plz olz plz plz
zvonat [6]

REM PROGRAM TO DISPLAY AVERAGE OF TWO NUMBERS

CLS

INPUT “ENTER FIRST NUMBER”; A

INPUT “ENTER SECOND NUMBER”; B

INPUT “ENTER THIRD NUMBER”; C

AV = (A + B + C) / 3

PRINT “AVERAGE OF THREE NUMBERS”; AV

END

hope this helpz

6 0
3 years ago
Ano kaya ang nararamdaman mo at mangyari sayo kung tatlong araw ka nang hindi kumakain​
Feliz [49]

Ang mararamdaman mo ay pananamlay at tila walang lakas. Ang mangyayari sayo ay magkakasakit ka at pag ipinagpatuloy mo parin ang hindi pag kain, baka hindi lang sakit aabotin mo kundi kamatayan.

3 0
3 years ago
Other questions:
  • In the Remote Access Domain, if private data or confidential data is compromised remotely, you should set automatic blocking for
    5·1 answer
  • What was the partition style(s prior windows server 2008 and windows vista?
    13·1 answer
  • True / False<br> 1. A byte is a standardized unit of measure that is always 8-bits.
    7·1 answer
  • Mihaela I. Croitoru – Utilizarea calculatorului personal Microsoft Word dau coroana
    14·1 answer
  • What connectors are available for components to be connected externally to the motherboard
    12·2 answers
  • Hat is the purpose of the domain name?
    7·2 answers
  • What programming language does the LMC 'understand'?
    5·1 answer
  • What is the output of this program? Assume the user enters 2, 5, and 10.
    6·1 answer
  • Working mechanism of computer ​
    10·1 answer
  • How has 5G become more energy efficient?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!