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
algol13
2 years ago
5

Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(l

ong n) For example, sumDigits(234) returns 9 the result of 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, which is 4. To remove 4 from 234, use 234 / 10, which is 23. Use a loop to repeatedly extract and remove the digit until all the digits are extracted.
Computers and Technology
1 answer:
kifflom [539]2 years ago
7 0

Answer:

The java program for the given scenario is shown below.

import java.util.*;

import java.lang.*;

public class Test

{

   //variables to hold the number, digits and sum of the digits

   //variable to hold number is assigned any random value

   static long num=123;

   static int sum=0;

   static int digit;

   static int s;

   //method to add digits of a number

   public static int sumDigits(long n)

   { do

       {

           digit=(int) (n%10);

           sum=sum+digit;

           n=n/10;

       }while(n>0);

       return sum;

   }

public static void main(String[] args) {

    s = sumDigits(num);

 System.out.println("The sum of the digits of "+num+ " is "+s);  }

}

OUTPUT

The sum of the digits of 123 is 6

Explanation:

1. The variables to hold the number is declared as long and initialized.

2. The variables to store the digits of the number and the sum of the digits are declared as integer. The variable, sum, is initialized to 0.

3. The method, sumDigits(), is defined which takes a long parameter and returns an integer value. The method takes the number as a parameter and returns the sum of its digits.

4. Inside method, sumDigits(), inside the do-while loop, the sum of the digits of the parameter is computed.

5. Inside main(), the method, sumDigits(), is called. The integer value returned by this method is stored in another integer variable, s.

6. The sum of the digits is then displayed to the console.

7. All the variables are declared outside main() and at the class level and hence declared static. The method, sumDigits(), is also declared static since it is to be called inside main().

8. In java, the whole code is written inside a class since java is a purely object-oriented language.

9. In this program, object of the class is not created since only a single class is involved having main() method.

10. The program can be tested for any value of the variable, num.

11. The file is saved as Test.java, where Test is the name of the class having main() method.

You might be interested in
To format an individual sparkline, select the sparkline you want to format, and then click the Ungroup button in the Group group
vichka [17]

Answer:

The answer is "Option a"

Explanation:

A Sparkline is a small graph, which is available inside a sheet, that displays information visually. It displays the information is in the form of patterns on several values. It also uses underline to display the low and high value, use with sparklines, and certain alternatives were wrong, which can be defined as follows:

  • In option b, It is used to define the overall view of data, that's why it is wrong.
  • Option c and Option d both are wrong because, create command is used to create data, or view is used to show that data, they both don't use sparkline option.  
3 0
3 years ago
Using complete sentences post a detailed response to the following.
KatRina [158]

Answer:A couple disadvantaged are the migraines that is can cause. And it can cause eye strain there are alot of things that could hurt your eyes. Like being in virtual reality for to long. It can very rarely make you go blind.

Explanation: sorry if its wrong

8 0
2 years ago
What guidance identifies federal information security controls?
Vladimir79 [104]

Answer:

<u>FIPS</u> <u>Publication</u> <u>2</u><u>0</u><u>0</u>, the second of the mandatory security standards, specifies minimum security requirements for information and information systems supporting the executive agencies of the federal government and a risk-based process for selecting the security controls necessary to satisfy the minimum security..

Explanation:

Hope it helps you..

Your welcome in advance..

(ㆁωㆁ)

3 0
2 years ago
Describe the objectives of e-commerce ?​
AnnZ [28]

Answer: eCommerce is to reach the more and right customers at the right time so that more orders can be placed and in turns, high revenue can be generated.

Explanation:

5 0
3 years ago
Why is the GPS so important today?
SIZIF [17.4K]

It allows us to precisely tell where places, people (under certain circumstances), etc. are. It is the most widely relied upon mapping system in the world. Using a GPS, you can find almost any plotted (known) location on the Earth. Most mapping operations rely on a GPS and would, otherwise, fail without it.

7 0
3 years ago
Other questions:
  • An example of hardware is a _____. database spreadsheet monitor program used to enhance photos
    13·2 answers
  • You have a new phone. What determines what type of messages you can send?
    15·1 answer
  • What happens when a magnet is close to a iron nail?
    12·1 answer
  • Exceptions can be handled in all of these ways except:
    6·1 answer
  • Which of the following is a benefit, as well as a risk, associated with peer-to-peer networks?
    6·2 answers
  • While (e &lt; 10):<br> print (c)
    10·1 answer
  • My mom hid my laptop and now I can't find it​
    9·1 answer
  • Plz subscribe my yt gaming channel <br>FIREAZZ GAMING​
    8·2 answers
  • 6.8 Code Practice<br> please can have some help please
    8·1 answer
  • What is contained in the Open Files section of Shared Folders? ​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!