Question

In: Computer Science

Write a Java program that reads a whole number on the keyboard and obtains and displays...

Write a Java program that reads a whole number on the keyboard and obtains and displays twice and triple that number on the screen

Solutions

Expert Solution


import java.util.Scanner;

public class WholeNumber {

        public static void main(String[] args) {
                
                int wholeNumber;
                Scanner sc = new Scanner(System.in);
                
                // reading whole number until valid entry
                while(true) {
                        System.out.print("Enter a whole number : ");
                        wholeNumber = sc.nextInt();
                        // if number is valid then break
                        if(wholeNumber >= 0)
                                break;
                        System.out.println(wholeNumber + " is not a whole number");
                }
                
                // doubling the number 
                int twice = 2 * wholeNumber ;
                System.out.println("Twice of "+wholeNumber+"  = "+twice);
                
                // tripling the number
                int triple = 3 * wholeNumber; 
                System.out.println("Triple of "+wholeNumber+" = "+triple);
                
                sc.close();
        }

}

Code  

Output  


Related Solutions

Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Write a Java program that prompts for and reads the number N of cities or locations...
Write a Java program that prompts for and reads the number N of cities or locations to be processed. It then loops N times to prompt for and read, for each location, the decimal latitude, decimal longitude, and decimal magnetic declination. It then computes and displays, for each location, the Qibla direction (or bearing) from Magnetic North. The true bearing from a point A to a point B is the angle measured in degrees, in a clockwise direction, from the...
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT