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
Why should you make sure the paper being used in a printer is dry and not damp?
Goryan [66]

Answer:

because the printer ink is liquid

Explanation:

you need a dry paper to let the ink sit in place and print out the exact same thing that u needed to print if you're printing on a damp paper there is a chance it will affect the output of whatever your printing

6 0
2 years ago
8.10 Code Practice Question 3
Alchen [17]

terms = ["Bandwidth", "Hierarchy", "IPv6", "Software", "Firewall", "Cybersecurity", "Lists", "Program", "Logic",

        "Reliability"]

def swap(arr, in1, in2):

   w = arr[in1]

   arr[in1] = arr[in2]

   arr[in2] = w

print(terms)

swap(terms, 5, 1)

swap(terms, 2, 4)

swap(terms, 3, 5)

swap(terms, 5, 6)

swap(terms, 6, 8)

swap(terms, 8, 9)

print(terms)

This is how I interpreted the question. If I need to make any changes, I'll do my best. Hope this helps though.

4 0
3 years ago
Whats the best way to get into a computers programming to get past firewalls
raketka [301]

you can use the software psiphon

6 0
3 years ago
Read 2 more answers
19. in the array implementation of a queue, the pop operation is most efficient if the front of the queue is fixed at index posi
ICE Princess25 [194]

It is true that in the array implementation of a queue, the pop operation is most efficient if the front of the queue is fixed at index position. The correct option is a.

<h3>What is pop operation? </h3>

The removal of an element is referred to as a pop operation. Again, because we only have access to the element at the top of the stack, we can only remove one element. We simply take the top of the stack off.

A push operation decrements the pointer before copying data to the stack; a pop operation copies data from the stack before incrementing the pointer.

The pop operation in an array implementation of a queue is most efficient if the queue's front is fixed at index position.

Thus, the correct option is a.

For more details regarding pop operation, visit:

brainly.com/question/15172555

#SPJ1

6 0
1 year ago
What it the total resistance in a series circuit with two resistors of 100 ohms each?
snow_tiger [21]

Answer:

200Ω

Explanation:

In series circuits, you add the resistances.

3 0
2 years ago
Other questions:
  • give two main reasons that should be considered when preparing and deploying a functional restoral scenario.
    5·1 answer
  • How many bit does four gigabyte has
    8·2 answers
  • Enables businesses and consumers to share data or use software applications directly from a remote server over the Internet or w
    6·1 answer
  • What is the different between ethical and legal issues?​
    6·1 answer
  • Which is NOT a benefit of using visual aids?
    9·2 answers
  • 2.36 LAB: Warm up: Variables, input, and casting (1) Prompt the user to input an integer, a double, a character, and a string, s
    12·1 answer
  • categorize each job role as an administrative job or management job auditor director legal secretary payroll clerk etc ​
    8·1 answer
  • Create a file named homework_instructions.txt using VI editor and type in it all the submission instructions from page1 of this
    7·1 answer
  • Write a multi-way if statement that compares the double variable pH with 7.0 and makes the following assignments to the bool var
    11·1 answer
  • Blockchain is often associated with Bitcoin and the financial services industry. However, it is applicable to almost every indus
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!