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
3 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]3 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
Wich command converts a number to a string​
AfilCa [17]

Answer:

okay yes i agree

Explanation:

8 0
3 years ago
Regulatory and informational markers are easily identified through which features
Darina [25.2K]
Regulatory or informational markers are utilized to inform you with respect to circumstances, threats, or headings. They may show reefs, swim territories, speed zones, and so forth. They can be effectively distinguished by the orange groups on the top and base of each float.
3 0
3 years ago
Hiding/masking personal identifiers from a data set, so that the data set can never identify an individual, even if it is correl
Anettt [7]

Hiding/masking personal identifiers from a data set, so that the data set can never identify an individual, even if it is correlated with other data sets is known as anonymization.

<h3>What is anonymization?</h3>

The term anonymization is known as data masking and it is the standard solution in the case of data pseudonymisation. It is generally recognised by using masking and data is de- sensitised also that privacy could be maintained and private information remains safe for the support.

Data is generally identified by using masking and data is de- sensitised also that privacy could be maintained and private information remains safe for the support.

Therefore, Hiding/masking personal identifiers from a data set, so that the data set can never identify an individual, even if it is correlated with other data sets is known as anonymization.

Learn more about anonymization here:

brainly.com/question/4268168

#SPJ4

5 0
2 years ago
The _____ feature will allow users to view nonprinting formatting marks to aid in editing a document. View Alignment Show/Hide I
Annette [7]

The __Show/Hide Insert___ feature will allow users to view non printing formatting marks to aid in editing a document.

The answer is D. Show/Hide Insert

Let me know if this is correct

5 0
3 years ago
I need some questions and answers in spreadsheet​
yarga [219]

Answer:

you need to show what you need help with

Explanation:

5 0
3 years ago
Other questions:
  • Write a setInterval() function that increases the count by 1 and displays the new count in counterElement every 300 milliseconds
    12·1 answer
  • C programming question:
    12·1 answer
  • Functions that are built-in into PHP to perform some standard operations.
    15·1 answer
  • What cable should i be using to connect my android tablet to the pc?
    13·2 answers
  • Among the rights you have as a user of computing resources is the right to​ _______.
    12·2 answers
  • To print a range of cells in the active worksheet, click ____ in the settings area in the print gallery.
    14·1 answer
  • Which line of code will print I can code on the screen?
    13·1 answer
  • You have noticed that one of your DNS servers has possibly been compromised. You believe that a cached DNS entry for your domain
    8·1 answer
  • List three types of Software:
    15·1 answer
  • Select three advantages to using digital video.
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!