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
I love my baby boy and can someone help me with this
mamaluj [8]
Ice cream sandwich definitely
8 0
3 years ago
Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Assume also that stdin is a v
zaharov [31]

Answer:

// program in java.

import java.util.*;

// class definition

class Main

{// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // object to read input

Scanner scr=new Scanner(System.in);

 // ask to enter name

System.out.print("Enter Your name: ");

 // read name from user

String NAME=scr.nextLine();

// print message

System.out.println("Greetings,"+NAME);

     

   }catch(Exception ex){

       return;}

}

}

Explanation:

Read name from user with the help of scanner object and assign it to variable "NAME".Then print a Greetings message as "Greetings,NAME" where NAME will be  replaced with user's input name.

Output:

Enter Your name: Rachel                                                                                                    

Greetings,Rachel

5 0
2 years ago
What is the IP address and TCP port number used by the client computer (source) that is transferring the file to gaia.cs.umass.e
lesya692 [45]

Answer:

Explanation:

ae

5 0
3 years ago
52.
OLEGan [10]
For the first one its NOW, I'm not sure about the 2nd, the 3rd I think is penal code cases but i could be wrong, and the 4th one sounds like voting is the most logical answer. 
7 0
3 years ago
Can include the 5-tuple information, which is the source and destination ip addresses, source and destination ports, protocols i
Delicious77 [7]

<span>The answer is Firewall Log.  The </span>Logging tab is a tabled, real-time and up-to-date integration of the traffic logs generated by the Firewall. It will show all the connections that have been active on your computer.  <span>You can also use the Windows Firewall log file to monitor TCP and UDP connections and packets that are blocked by the firewall.</span>

7 0
3 years ago
Other questions:
  • Juliet is trying to increase her savings account and she decides that she is going save an additional $700 every month. If her m
    9·1 answer
  • Which file extension indicates that a file is an Adobe Acrobat document?
    13·1 answer
  • Lydia noticed that she feels tired and out of breath after walking her dog up a hill in the park. Which area of fitness does she
    8·1 answer
  • When you catch an Exception object, you can call ____ to display a list of methods in the call stack so you can determine the lo
    6·1 answer
  • Level of comfort that people feel
    10·1 answer
  • Websites that group individuals and organizations into clusters or groups based on some sort are considered to be what type of n
    11·1 answer
  • One key criterion for selecting social networks is the number of daily visitors to the website. when comparing linkedin traffic
    7·1 answer
  • Is there any quantum computer in India?​
    9·1 answer
  • Wanna song to vibe to? Listen to Streets by Doja cat
    11·1 answer
  • 72. In Object Oriented Programming, a class_____ starts
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!