Question

Divisibility by 9 To determine if a number is evenly divisible by 9, simply add all...

Divisibility by 9

To determine if a number is evenly divisible by 9, simply add all of the digits in that number. If the sum is divisible by 9, then the original number also is divisible by 9.

For example, notice that all of the multiples of 9 from 1 through 12 (9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, and 108) contain digits which sum to 9 or 18 in every case.

In another example, to determine if the number 544,293 is divisible by 9, add all of the digits in that number (5 + 4 + 4 + 2 + 9 + 3) which totals 27. Since 27 is divisible by 9, then so is 544,293.

In this part of the lab, write a program which does the following:

  1. Prompt the user for a whole number and read the user’s input into a String.
  2. Calculate the sum of the digits in the number by looping through each character (each digit) in the String, converting it to a number, and adding it to the sum.
  3. If the sum of the digits is 0, then exit the program
  4. Otherwise, determine if the sum of the digits in the number is evenly divisible by 9.
  5. Using that result, print a message to the user indicating whether or not the original number is divisible by 9.
  6. Go back to step 1.

Notice that this process has a way to stop (step 3 above), and that each step is well-defined and unambiguous, so this process satisfies our definition of an algorithm.

Submit a listing of your program, along with a PrintScreen showing what happens when you enter the following numbers: 9, 54, 84023, 333333, and the sentinel 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class DivisibleBy9 {
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String inp = "";
  
do
{
int sum = 0;
System.out.print("Enter a whole number: ");
inp = sc.nextLine().trim();
if(inp.equals("0"))
{
System.out.println("Sum of digits is 0..exiting program");
System.exit(0);
}
  
for(int i = 0; i < inp.length(); i++)
{
int digit = Character.getNumericValue(inp.charAt(i));
sum += digit;
}

if(sum == 0)
{
System.out.println("Sum of digits is 0..exiting program");
System.exit(0);
}
else
{
if(sum % 9 == 0)
System.out.println(inp + " is completely divisible by 9");
else
System.out.println(inp + " is not completely divisible by 9");
}
}while(!inp.equalsIgnoreCase("0"));
}
}

****************************************************************** SCREENSHOT *********************************************************

Add a comment
Know the answer?
Add Answer to:
Divisibility by 9 To determine if a number is evenly divisible by 9, simply add all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • Multiples of 9’s (JAVA) Using the logic behind the algorithm in Part 1, it’s easy to...

    Multiples of 9’s (JAVA) Using the logic behind the algorithm in Part 1, it’s easy to calculate the first nine multiples of 9 (9x1, 9x2, 9x3…, 9x9). Let’s use two examples. First, to determine the product of 9 times 6, perform the following algorithm: Step 1: Subtract 1 from the number being multiplied by 9 (in this case 6) to get the first digit of the product: 6 – 1 =  5. Step 2: Subtract that digit from 9 to get...

  • Open the Validate Number Solution.sln file contained in the VB2017\Chap07\Validate Number Solution folder. The interface provides...

    Open the Validate Number Solution.sln file contained in the VB2017\Chap07\Validate Number Solution folder. The interface provides a text box for entering a 9-digit number. The btnValidate_Click procedure should use the algorithm and example shown in Figure 7-56 to validate the user’s entry. The procedure should display a message indicating whether the entry is or is not valid. Code the procedure. Include any other code that will professionalize the interface. Save the solution and then start and test the application. Create...

  • C++: Write a recursive function that does the following: Given a number, add all the digits...

    C++: Write a recursive function that does the following: Given a number, add all the digits and display the sum. Example:                         The sum of the number 5432 would be 14. PLEASE PAY ATTENTION TO THE FOLLOWING: Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from...

  • *these questions are related to Matlab The number 24 is exactly divisible by eight numbers (i.e. 1, 2, 3, 4, 6, 8, 12 and 24). The number 273 is also exactly divisible by eight numbers (i....

    *these questions are related to Matlab The number 24 is exactly divisible by eight numbers (i.e. 1, 2, 3, 4, 6, 8, 12 and 24). The number 273 is also exactly divisible by eight numbers (i.e. 1, 3, 7, 13,21, 39, 91 and 273) There are 10 numbers in the range of 1:100 that are exactly divisible by eight numbers (i.e. 24, 30, 40, 42, 54, 56, 66, 70, 78 and 88). How many numbers in the range of n-1:20000...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

    JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT