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
4vir4ik [10]
3 years ago
13

6.2 Sum the digits in an integer (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 (= 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));
Computers and Technology
1 answer:
liberstina [14]3 years ago
4 0

Answer:

The method sumDigits() and the test program is given in the explanation section below:

Explanation:

import java.util.Scanner;

public class num8 {

   public static int sumDigits(long n){

       long sum = 0;

       while (n != 0)

       {

           sum = sum + n % 10;

           n = n/10;

       }

       return (int)sum;

   }

   public static void main(String[] args) {

       System.out.println("Enter a long integer");

       Scanner in = new Scanner(System.in);

       long digit = in.nextLong();

       System.out.println(sumDigits(digit));

   }

}

The steps required to implement this method are given in the question. A while is used to repeatedly extract and remove the digit until all the digits are extracted. In the main method, the Scanner class is used to prompt a user to enter a digit, the method is called and the digit is passed as an argument.

You might be interested in
from january 2005 through july 2015, approximately how many electronic data records in the United States were breached, exposing
Brums [2.3K]
From January 2005 through July 2017, approximately 853 million electronic data records in the US were breached.
This allowed the attackers to take control over people's personal data, such as their various credit card numbers, and other important data, as well as their addresses, etc. The cyber police, as well as the regular police have been working hard to stop this from happening, but the hackers are very strong and smart.
4 0
3 years ago
What is a layer, in the context of this lesson?
pshichka [43]

Answer:

A logical unit of a system.

Explanation:

i'm thinking your are talking about system layers such as the ISO? Layers help identify the order of how the whole system works.

7 0
3 years ago
Read 2 more answers
Identify 5 internal and external hardware components of a server
UNO [17]

Answer:

Internal:

#CPU; That retrieves &execute instructions.

#Modem; Modulates& demodulates electric signals.

#RAM;Gives application a place to store &access data on a short time periods.

External:

#Mouse; Transmits commands and controlling movements.

#Moniter; Device used to display video output from computer.

#Printer; Accepts text, graphics to the paper.

Explanation:

Hope this will help you.

5 0
3 years ago
What happens if you never confirm your facebook account?
OleMash [197]
Nothing, it will just keep sending you annoying notifications that become more and more frequent, i would just confirm it if i were you
5 0
3 years ago
Which option of the AutoCorrect tool enables you to add and delete words that do not follow abbreviation rules?
stellarik [79]

Answer:

Replace text as you type.

Explanation:

Replace text as you type is the Autocorrect option that enables you to add and delete words that do not follow abbreviation rules. This tool is available under the PROOFING tab. For example, if you use a long phrase frequently, simply add it to the replace text as you type. For instance, if you type your address frequently, add 34 Willoughby Street, London, and abbreviate it to 34WS and it was replace it to the long phrase as you type 34WS.

5 0
3 years ago
Other questions:
  • The following program includes fictional sets of the top 10 male and female baby names for the current year. Write a program tha
    15·1 answer
  • Explain how inflation flattens the universe
    6·2 answers
  • What can you use with your keywords to narrow your search if you complete an internet search using a search engine and do not ge
    15·2 answers
  • Write a program that takes an integer n as a parameter and sums all of the multiples of 2 or 5 from 1 to n. For example, if n =
    14·1 answer
  • Can someone please help me to point out what's wrong with this C program:
    13·1 answer
  • What are the characteristics of calendar sharing options in Outlook 2016? Check all that apply.
    13·2 answers
  • Lab 6B: printing a binary number
    13·1 answer
  • It is a data being transported on a network​
    13·1 answer
  • Quick question if anyone knows? How do you give brainlist? Please tell me if you know! ( Here if a chiaki gif for your too lol )
    5·1 answer
  • Is Using programming libraries is one way of incorporating existing code into new programs true
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!