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
When planning your App what difficulties did come across and why?
RSB [31]

Answer:

1. An engaging app idea

2. Sufficient funds

3. Cross-platform apps

4. Interactive apps

5. Application content management

ETC...

7 0
2 years ago
Read 2 more answers
Letter only ^_____^!
vagabundo [1.1K]

b serves as food for the yeast

4 0
3 years ago
3) Write a program named Full_XmasTree using a nested for loop that will generate the exact output. This program MUST use (ONLY)
N76 [4]

Answer:

Following are the code to this question:

//import package

import java.util.*;  

public class Full_XmasTree   //defining class

{        

// defining main method

public static void main(String as[])  

   {  

       int X,a,b; //defining integer variable

       Scanner obx = new Scanner(System.in); // creating Scanner class object

       System.out.print("Please enter: "); //print message

       X = obx.nextInt(); // input value from user

       for (a = 0; a < X; a++) //defining loop to print pattern  

       {

           for (b = X - a; b > 1; b--)//use loop for print white space  

           {

           System.out.print(" ");//print space

           }

           for (b = 0; b <= a; b++) // use loop to print values  

           {

               System.out.print("* "); //print asterisk values

           }

           System.out.println(); //using print method for new line

       }

   }

}

Output:

please find the attachment.

Explanation:

In the given java code, a class "Full_XmasTree" is declared, in which the main method is declared, inside this method three integer variable "X, a, and b", in this variables "a and b" is used in a loop, and variable X is used for user input.

  • In the next line, the Scanner class object is created, which takes input in variable X, and for loop is used to print asterisk triangle.
  • In the first for loop, use variable a to count from user input value, inside the loop, two for loop is used, in which first is used to print white space and second is used for the print pattern.

8 0
3 years ago
A Ground Fault Circuit Interrupter (GFCI) works by comparing the amount of current going to and returning from equipment along t
Shtirlitz [24]

As the name states, during a ground-fault (when the current going out is significantly different than the current going in) the GCFI interupts the current.  Usually by a switch of some sort.  (Pushing the "test" button produces that click, and cuts off the current)

7 0
3 years ago
Read 2 more answers
A student is helping a friend with a home computer that can no longer access the Internet. Upon investigation, the student disco
Alexxandr [17]

Answer:

Due to unreachable DHCP server

Explanation:

As we know that Dynamic Host Configuration Protocol, in short  DHCP is the network protocol which is used to central and automatic management of IP address with in the network.

So due to unreachable DHCP server a student could not able to connect with the internet.When the network connection break it means that DHCP server is offline.

So the answer is Due to unreachable DHCP server .

8 0
3 years ago
Other questions:
  • In a social networking site your personal information is listed under your inbox. home. status. profile.
    11·1 answer
  • What component on a smartphone requires pairing with another device?
    8·1 answer
  • What is ment by creative middle way solution
    6·1 answer
  • The True Confessions of Charlotte Doyle begins with what is called a(n) _____.
    15·1 answer
  • Select the correct answer.
    10·2 answers
  • When an organization uses cloud computing, they do not have to buy and maintain expensive hardware. Group of answer choices True
    6·1 answer
  • Look at the network topology. What will happen with the other computers if the computer breaks at the RED X?
    8·1 answer
  • How to make a harmonic mean algorithm in bash Linux?
    15·1 answer
  • How to create a network of relevant prospects?
    5·1 answer
  • A network technician attempts to ping www.example.net from a customer computer, but the ping fails. access to mapped network dri
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!