Question

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 the second digit: 9 – 5 = 4.
Result: Put the two digits together to get the answer:  54

To determine the product of 9 times 3, perform the following:

Step 1: Subtract 1 from the number being multiplied by 9 (in this case 3) to get the first digit of the product: 3 – 1 =  2.
Step 2: Subtract that digit from 9 to get the second digit: 9 – 2 = 7.
Result: Put the two digits together to get the answer:  27

Using the pattern described in the above two examples, write a program which prompts for and reads an int between 0 and 9 from the user, then calculates and displays that number multiplied by 9. The program then loops back and asks for another number from the user.

Please note that the number 0 may require some special programming, as the above algorithm may not work properly.

Your program should handle any number entered by the user.

  • If the user enters the number -1 (the sentinel), then exit the program.
  • If the user enters a number between 0 to 9, first determine number multiplied by 9 as described above, then go back to the top of the program to prompt for the next number. (The number 0 may require some special processing).
  • Otherwise print an error message telling the user that the number is too large or small, then loop back and prompt for the next number.

Submit all Java programfiles, along with PrintScreens showing the results of entering the following numbers: 0, 3, 6, 9, 10, and the sentinel -1.

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

Code


import java.util.Scanner;


public class Mian {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num,a,b,ans;
while(true)
{
System.out.print("Enter a number between 0 - 9 or -1 to stop: ");
num=sc.nextInt();
  
if(num==-1)
break;
if(num>=0 && num<=9)
{
a=num-1;
b=9-a;
ans=a*10+b;
System.out.println("The product of 9 times "+ num +" is "+ans);
}
else
{
System.out.println("Invalid input. Please enter the number between 0 and 9 inclusive.");
}
System.out.println();
}
}
  
}

Output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Multiples of 9’s (JAVA) Using the logic behind the algorithm in Part 1, it’s easy to...
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
  • 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...

  • 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...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Please make a JAVA program for the following using switch structures Write a program that simulates...

    Please make a JAVA program for the following using switch structures Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the...

  • Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from...

    Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print “4 2 3 3 9.” (Hint: The number input is five digits long, dividing it by 10000 gives the leftmost digit. digit1 = number / 10000 ) b. determines...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • C PROGRAM A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by...

    C PROGRAM A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by comparing the code’s last digit (called a check digit) to its own computation of the check digit from the first 11 digits as follows: 1. Calculate the sum of the digits in the odd-numbered positions (the first, third… eleventh digits) and multiply this sum by 3. 2. Calculate the sum of the digits in the even-numbered positions (the second, fourth… tenth digits) and add...

  • Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase....

    Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons. Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

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