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]
3 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]3 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
PLS CAN ANYONE HELP ME
Nesterboy [21]

The exercise contains 15 questions. The solution is provided for each question.

Each question contains the necessary skills you need to learn.

I have added tips and required learning resources for each question, which helps you solve the exercise. When you complete each question, you get more familiar with a control structure, loops, string, and list.

Use Online Code Editor to solve exercise questions.

Also, try to solve the basic Python Quiz for beginners

Exercise 1: Given two integer numbers return their product. If the product is greater than 1000, then return their sum

Reference article for help:

Accept user input in Python

Calculate an Average in Python

Given 1:

number1 = 20

number2 = 30

Expected Output:

The result is 600

Given 2:

number1 = 40

number2 = 30

Expected Output:

The result is 70

Explanation:

4 0
2 years ago
What email server does Global Enterprises use?
harina [27]

Answer:

Global Enterprises uses an open source mail server called Courier.

Explanation:

6 0
3 years ago
Which sentence describes the right thing to do when the computer doesn’t respond?
PolarNik [594]
3 is correct because if brings you to the lock screen and lets you restart the computer
 1 is wrong because if you disconnect it there is a chance if won't connect and power it anymore.
5 0
2 years ago
Read 2 more answers
Binary Number Cards
Sedaia [141]

Answer:

13: 1101

5: 0101

10: 1010

2: 0010

Explanation:

The binary system, on the other hand, is a base-2 number system. That means it only uses two numbers: 0 and 1. When you add one to one, you move the 1 one spot to the left into the twos place and put a 0 in the ones place

what is done with the dice is to have an example of how binary numbers are made until 15

5 0
2 years ago
Back wit another one, help !?<br><br>these are two different questions btw
trapecia [35]
Question 1: Best way is option 1, Worst way is option 4.

Question 2: Best way is option 4, Worst way is option 3

7 0
2 years ago
Other questions:
  • For what show did the actor, whose star is located at 6667 Hollywood Boulevard, earn an Emmy Award for Outstanding Supporting Ac
    10·2 answers
  • How can i add card reader to pc answers?
    9·1 answer
  • Which of the following types of access controls do not describe a lock? (a)- Directive (b)- Physical (c)- Preventative (d)- Dete
    8·1 answer
  • The TabIndex value assigned to a text box's identifying label must be _______________ for the text box's access key to work corr
    14·1 answer
  • A study guide can be created
    6·2 answers
  • In Microsoft Word, spelling errors are identified by a _____.
    5·2 answers
  • This is used in a program to mark the beginning or ending of a statement, or separate items in a list:_____
    15·1 answer
  • Suppose that a disk drive has 5,000 cylinders, numbered 0 to 4,999. The drive is currently serving a request at cylinder 2,050,
    14·1 answer
  • A unique ability of people which sets them far apart from animals is:
    8·1 answer
  • I am a new linux and unix system user. how do i find out the user and groups names and numeric ids of the current user or any us
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!