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
statuscvo [17]
2 years ago
13

Write a program that will take an integer and add up each of the number’s digits, count the number of digits in the number, and

then average the digits. You must use a while loop and % to access each of the individual digits. Use / to reduce the number so that you can average all of the digits.
Example: num = 123 can be taken apart with tempNum = 123 % 10 (which will pull off the 3), then tempNum = tempNum / 10 (will result in 12). Do this in a loop while the number is > 0.
 
Sample Data :
234
10000
111
9005
84645
8547
123456789
 
 
 Be sure to include print statements that will format the output as below.

Sample Output :
234 has a digit average of 3.0
 
10000 has a digit average of 0.2
 
111 has a digit average of 1.0
 
9005 has a digit average of 3.5
 
84645 has a digit average of 5.4
 
8547 has a digit average of 6.0
 
123456789 has a digit average of 5.0

Computers and Technology
2 answers:
Inga [223]2 years ago
5 0
For count digits, you could just convert it to a String and check the length
Sum digits, convert to string then seperate each character with charAt then convert it to numbers in the return statement.
Average digits you can convert it to a String and then convert them back after taking them apart.
STALIN [3.7K]2 years ago
5 0
Here is my solution:

import java.lang.System.*;

public class DigiMath {   

private static int countDigits(int number)
{
       int sum = 0;
       while(number > 0)
       {
           sum ++;
           number /= 10;
       }       return sum;   
}

private static int sumDigits(int number)   
{       
      int sum = 0;       
      while(number > 0) {
           sum += number % 10;
           number /= 10;
       }
       return sum;
}
   
   public static double averageDigits( int number )
   {
       int count = countDigits(number);
       if (count > 0) {
           return (double)sumDigits(number) / (double)count;      
       } else { 
            return 0; 
       } 
   }
   
   public static void PrintDigitAverage(int number)
   {
       System.out.printf("%d has a digit average of %1.1f\n", number, averageDigits(number));
   }
   
   public static void main(String[] args) 
   {
       // Method tests (run with java -ea)
       assert countDigits(12345) == 5 : "countDigits returns wrong count";
       assert sumDigits(12345) == 15 : "sumDigits returns wrong sum";
       assert averageDigits(12345) == 3.0: "averageDigits returns wrong average";

        PrintDigitAverage(234);
        PrintDigitAverage(10000); 
        PrintDigitAverage(111); 
        PrintDigitAverage(9005); 
        PrintDigitAverage(84645); 
        PrintDigitAverage(8547); 
        PrintDigitAverage(123456789);
   }
}


You might be interested in
A USB is used for _________.[ pick the best answer that makes sense]
Nookie1986 [14]
<span>B) storing information and documents, pictures</span>
4 0
3 years ago
Read 2 more answers
Which is an example of a Boolean operator that evaluates as TRUE?
ladessa [460]

The expression NOT (3+2=7) evaluates as TRUE because is is NOT the case that 3+2=7.

Let me know if you have any questions.

4 0
2 years ago
The animation industry is solely reliant on 3-D modeling and 3-D virtualization to create the animated movies in the cinemas.
loris [4]
False it is false people also hand draw stuff
7 0
3 years ago
HELP PLEASE!! WILL MARK FIRST AND MOST RELIABLE ANSWER BRAINLIEST!!~~~~What is the primary difference between sort and filter?
tatiyna

Answer:

A

Explanation:

Example: when scrolling through a list of shows, if you filter for action shows, only shows that match the action description would appear

3 0
3 years ago
Which statement is true for a career as a graphic designer?
Brrunno [24]

Answer:

The answer is B.

Explanation:

As a graphic designer you are responsible for almost anything you do! :)

Hope this helps

6 0
3 years ago
Other questions:
  • An archive of files that usually contain scripts that install the software contents to the correct location on the system is ref
    11·1 answer
  • Which algorithm makes the most efficient use ofmemory?
    9·1 answer
  • What allows a person to interact with web browser software?
    13·2 answers
  • The chip that controls the radio frequency waves within a device
    9·1 answer
  • It would be system unit on this test
    7·1 answer
  • You can combine the algorithms for converting between infix to postfix and for evaluating postfix to evaluate an infix expressio
    13·1 answer
  • In which table is the input and the corresponding output of a Boolean function listed? How is the output of the NAND gate determ
    9·1 answer
  • Edhesive, 8.6 question 1
    10·1 answer
  • You are purchasing a new printer. Which of the following is the most important requirement?
    5·1 answer
  • Computers rarely make mistakes. True or false
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!