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
A user informs you that he or she never deletes any files he or she creates. How might this affect the operating system? It will
Juliette [100K]

Answer:

Depending on the amount of files there are, the operating system will run slower because these files take up space on the hard drive, slowing it down.

Explanation:

6 0
2 years ago
What allows a programmer to write code quickly and efficiently for an action that must be repeated?
Marta_Voda [28]
An literation :))!! i hope u get it right lol
6 0
2 years ago
Read 2 more answers
Which of the following SNSs has been associated with art work display?
amid [387]
Hoi!

I would say DeviantArt, since that is the world's largest social website dedicated to displaying artwork.
7 0
2 years ago
Read 2 more answers
Open this link after reading about Ana's situation. Complete each sentence using the drop-downs. Ana would need a minimum of ato
Arte-miy333 [17]

bachelor degree and grow

6 0
3 years ago
Read 2 more answers
How drone technology can impact recreation and entertainment​
Maurinko [17]

Drones are become more advanced than ever before. Are now, they are starting to impact recreation. They are small, light, and can maneuver incredibly fast. They are being used in movies, skits, short films, and even high-end videos. Here are some ways drones are changing entertainment:

- Providing high-quality filming, even from 200 feet in the air.

- Reaching spaces where bulky helicopters cannot fit into safely

- Laser shows, where drones shine lights and lasers.

Right now, if you tried to hold your phone still to capture a video, you would not be able to do it. Upon checking the footage, you would see the camera shaking, even if it's just tiny bit. While you can lean it on a surface to fix this, it simply cannot be done in the air to capture. Not to worry, though, you've got drones. They can shoot a steady shot 200 feet in the air. The only thing that could rival drones are expensive, bulky choppers. The drones are cheaper and are mass produced.

However, drones are not only being used to filming movies and films. They are also being used for a different kind of entertainment: Shows. Drones move in formation and shine lights in the night sky, like we saw in the Winter Olympics. They are truly stunning(pics attached).

7 0
1 year ago
Other questions:
  • Most switches enable you to use multiple nics for a single machine, a process called __________ or link aggregation.
    11·1 answer
  • 1. Write a bash script to create 3 files of different size greater than 1700 kb and less than 1800 kb . The 2. Content of the fi
    9·1 answer
  • Why are computer messages encapsulated?
    13·1 answer
  • How is a recession determined?
    10·1 answer
  • What are the seven internal compontents of a computer
    10·1 answer
  • Which represents the hierarchical structure of a Google Analytics account from top to bottom?
    5·1 answer
  • Is technology science? I mean, I think it is... uh, why am I so paranoid about this project.
    12·2 answers
  • Which wireless standard runs on both the 2.4 and 5 GHz frequencies?
    11·1 answer
  • What is one of the limitations of marketing in social media?.
    13·1 answer
  • (e) The entries in each column of the array A are sorted into strictly increasing order.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!