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
8. Given the array String[] words, which already contains 1 or more values, write a block of code which counts and returns the n
Vikentia [17]

Answer:

Following are the code to this question:

public class Main//defining a class

{

public static void main(String[] arg)//defining main method

{

  String[] words={"Key","day", "Know", "kind"};//defining array of String words

  int x=0;//defining integer variable for count value

  for(int i=0;i<words.length;i++)//defining for loop for count value

  {

 if(words[i].startsWith("k")||words[i].startsWith("K"))//use if block to check word start from k

  x=x+1;//increment the value of x

  }

System.out.print("The number of letters which starts from k is: "+ x);//print value with message

}

}

Output:

The number of letters which starts from k is: 3

Explanation:

In this code, inside the main method an array of String "words" is defined that hold a value, and in the next step an integer variable "x" is defined, which is used to count the letters, which starts from k.

For this, a for loop is used that counts the value and in this an, if block is defined that uses the "startsWith" method to check the value and increment the value of x and at the last, it prints its value.

7 0
3 years ago
Can any existing directory beneath the system root directory be used as a mount point?
musickatia [10]
<span>Yes.
   A mount point mounts a capacity gadget or filesystem, making it available and appending it to a current registry structure.
    While an umount point "unmounts" a mounted filesystem, illuminating the framework to finish any pending read or compose activities, and securely confining it.</span>
7 0
3 years ago
You've been asked to design a soho network on a work place. the other offices in the building also have wireless networks. while
Kipish [7]

In the scenario in which you should to design a SOHO network on a work place. the other offices in the building also have wireless networks. while setting up a wireless network for the SOHO, your initial step should be to run a wireless site survey.

This survey will help you to know which access points (AP) are in the most optimal locations.

5 0
3 years ago
What type of information is appropriate for headers and footers? Check all that apply.
lbvjy [14]

Answer:

slide/page number

smart art

maybe date and time

Explanation:

6 0
3 years ago
Read 2 more answers
How can you check an orthographic drawing to be sure there are no missing lines
user100 [1]
With an magnify glass
4 0
3 years ago
Other questions:
  • Under the common criteria, which term describes the user-generated specifications for security requirements?
    12·1 answer
  • What are the 6 external componets to a computer?
    7·1 answer
  • Filtering removes data from the spreadsheet. A. True B. False
    9·2 answers
  • Which of the following might not exist in a URL?
    10·2 answers
  • Computer user support helps people with minor computer problems. <br><br> True or False
    12·1 answer
  • if you are trying to reduce your debt, which of these expenses should you consider cutting out or cutting back on first?
    11·1 answer
  • Seven Features of computer aids design software
    6·1 answer
  • Import java.util.ArrayList;
    14·1 answer
  • Describe how you will lunch a web browser using the start menu​
    6·2 answers
  • A/an _____________ is a stored program that executes at a specified time. Group of answer choices stored procedure view trigger
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!