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
Binary is best interpreted by a computer because ​
Nookie1986 [14]
Because it is the back-end programming. It is the basis of all of the other computer languages and allows the computer to function.
8 0
3 years ago
Read 2 more answers
The difference between a want and a need is a want is not necessary for survival. Things necessary for survival are known as ___
Zarrin [17]
A.Needs since they are necessary and needed for survival 
8 0
3 years ago
Explain the importance of signaling in the telephone network.
k0ka [10]

Answer: Please see below as the answer is self-explanatory

Explanation:

In a telephone system, we call signaling to all signals that are originated within a telephony circuit, that are not part of the conversation between the calling parties.

We include in this broad category, the signal generated by the calling party when he picks up the phone, the call tone sent to him by the switch (or the busy tone if the call can't be established due to blocking), the digits that identify the called party, the ringing signal that informs the called party that someone is trying to reach him for a call, among others like are defined in the SS7 or the different 2G/3G/4G Cellular standards.

The importance of the signalling is pretty obvious; without these signals, it will be virtually impossible to set up a connection between the two parties.

8 0
4 years ago
In the context of customer relationship management (CRM) applications, which approach is often chosen by organizations with an e
Bad White [126]

The on-premise approach is often chosen by organizations with an established IT infrastructure.

<h3>What is the On Premises?</h3>

In an on-premises method, resources are said to be given or deployed in-house and it is one that is found within an enterprise's IT infrastructure.

Customer relationship management (CRM) is known to be a form of a technology that is often used in the act of managing all of a firm's  relationships and communications with customers and also that of potential customers.

Therefore, The on-premise approach is often chosen by organizations with an established IT infrastructure.

Learn more about CRM from

brainly.com/question/27373018

#SPJ1

5 0
2 years ago
Tanya purchases a new pair of shoes for $45. The cashier asks her to pay $48. Which of these explains the difference in price?
harina [27]

Answer:

An indirect tax in the form of sales tax

Explanation:

This is certainly the sales tax, and that the shoe retailer is charging with the actual price And each retailer must charge this as they have to submit the sales tax on the number of goods they sell in a year. And these are indirect taxes. Hence, the $45s here is the price of the shoe, and the extra $3s is the sales tax on that shoe. The retailer collects this on behalf of the government, and remember this is an indirect tax.

8 0
3 years ago
Other questions:
  • Tasha purchased a new tablet. She has several questions about how to connect to the internet, download apps, install software, a
    5·1 answer
  • The __________ is the continuity of control of evidence that makes it possible to account for all that has happened to evidence
    14·2 answers
  • How is a cell named?
    9·1 answer
  • What type of media can be edited and published by anyone?
    6·2 answers
  • You easily can give slides in a presentation a professional and integrated appearance by using a placeholder.
    11·1 answer
  • Rom also called main memory or system memoryis used to stor the essential parts of the operating while the computer is running /
    6·1 answer
  • Which of the following statements applies to a proprietary software license? a. Purchased from a vendor and gives you the right
    5·1 answer
  • 2. Define a function squareArea that computes the area of a square given side length
    14·1 answer
  • 12. Why is it so important to pay attention to your digital reputation?
    14·1 answer
  • What are two advantages of edge computing over cloud computing?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!