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]
3 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]3 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]3 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
What type of malicious procedure involves using sniffing tools to capture network communications to intercept confidential infor
Alex_Xolod [135]

Answer:

eavesdropping

Explanation:

Eavesdropping is as an <em>electronic attack</em> where digital communications are intercepted by an individual whom they are not intended. This is done in two main ways: <em>Directly listening</em> to digital or analog voice communication or the <em>interception or sniffing</em> of data relating to any form of communication.

4 0
3 years ago
What does the minimum password age setting control?
Dmitrij [34]
13? 13 is most minimum ages however, I don't understand this question all that well.
3 0
3 years ago
The definition of an "analog device" is that it is a type of _____.
sveta [45]
ITS C hope it helps goo luck!
5 0
3 years ago
Imagine that you are preparing a multimedia presentation. What are the four things you need to consider when getting started?
Marrrta [24]

Explanation:

Consider the Content

Create an Outline

Develop Your Presentation

PRACTICE!!

8 0
3 years ago
Read 2 more answers
Wow that funny a heck
lorasvet [3.4K]

Answer:

Seriously??? Omg - Orange Trump!!

3 0
2 years ago
Other questions:
  • A technician is troubleshooting a computer that will not communicate with any hosts on the local network. While performing a vis
    9·1 answer
  • An sObject named Application _c has a lookup relationship to another sObject named Position_c.
    12·1 answer
  • A marketing associate wants to use the Validate button to ensure an email is CAN-SPAM compliant. What information does the assoc
    5·2 answers
  • Write a program in C++ or C that includes two different enumeration types and has a significant number of operations using the e
    15·1 answer
  • Code embedded into an HTML page and downloaded by a user; resides on the client and helps process Web form input. Common clients
    9·1 answer
  • Two categories of payroll deductions are required deductions and ___ deductions.
    9·2 answers
  • Create a text file named employee.dat containing the following data: b. Write a C++ program to read the employee.dat file create
    6·1 answer
  • Pls help I will give lots of points
    9·1 answer
  • What is Celeste? ( This is for my coding class )
    6·2 answers
  • Will give brainliest!<br> Who created binary, and does anyone have any idea how?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!