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
How can E-Commerce Portals ensure Security of online Transactions by Customers?
beks73 [17]
Ffkjlfdhaslkjfhlaksdfhlkajsdfh
6 0
3 years ago
Please solve this,this is a scratch 2.0 question<br><br>The _______ shows the output of a script ?​
rusak2 [61]

Answer:

Console

Explanation:

Not sure exactly what scratch 2.0 is but the console is a separate window that shows the script's outputs and in some cases collect inputs, show error messages or display other info depending on what compiler you used

7 0
3 years ago
The PictureBook class is a subclass of the Book class that has one additional attribute: a String variable named illustrator tha
bulgar [2K]

Answer:s

s

Explanation:

x

8 0
4 years ago
Emma wants to create a web page for her school’s volleyball team. Which of these could she use?
ser-zykov [4K]

Answer:

Emma should use Chrome

3 0
3 years ago
8085 microprocessor​
ohaa [14]

do you want us to write something about it or??

5 0
4 years ago
Other questions:
  • D. The 7-bit ASCII code for the character '&amp;' is:
    7·1 answer
  • The conscious process of planning and controlling how you spend your time is called?
    15·1 answer
  • If the force of gravity _ then the weight of an object will _
    8·2 answers
  • What does this mean?
    14·1 answer
  • Can someone help me out with this question?​
    9·1 answer
  • ap csp The local, remote, and upstream _______ can each have multiple ___ _____. When a participant in a collaborative group on
    5·1 answer
  • Naynar kis dhrm se sambandhit hai​
    15·2 answers
  • Using a custom animation effect, how do you make text appear on a slide letter by letter?
    9·1 answer
  • I can login to it says to me u cant login at that time
    10·2 answers
  • What should the Dhruv consider when connecting to the Internet in a public place? Select all that apply.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!