Question

Write an application called Squaring that will: • Prompt the user to enter an integer greater than 1. Your code for this shou

in java

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

import java.util.Scanner;

public class Squaring {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int num = 0, k = 1, count = 0;
       while (k == 1) {
           k = 0;
           System.out.println("Enter an integer greater than 1:");
           if (sc.hasNextInt()) {
               num = sc.nextInt();
               sc.nextLine();
               if (num <= 1) {
                   System.out.println(num + " is not greater than 1.");
                   k = 1;
               }
           } else {
               System.out.println(sc.nextLine() + " is not a valid input.");
               k = 1;
           }
       }
       double sq = num;
       while (sq <= 1000000) {
           sq = sq * sq;
           System.out.printf("%.0f\n",sq);
           count += 1;
       }
       System.out.println(num + " exceeded 1,000,000 after " + count + " squarings.");
       sc.close();
   }
}

Enter an integer greater than 1 j is not a valid input Enter an integer greater than 1: 5.0 5.0 is not a valid input. Enter a

Add a comment
Know the answer?
Add Answer to:
in java Write an application called Squaring that will: • Prompt the user to enter an...
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 a complete program that will prompt the user for an integer You must keep asking...

    Write a complete program that will prompt the user for an integer You must keep asking the user until they give you a number that is greater than 700. Once they give a valid number, display all multiples of 7 from 7 to that number inclusively). You may assume the user will give you correct type of input, but your code must continue to prompt them until they give a number greater than 700.

  • 1) Write Single-line comments for each code in programming C++ 2) Prompt the user to enter...

    1) Write Single-line comments for each code in programming C++ 2) Prompt the user to enter a number from 1 to 100, inclusive. If the number is not valid, meaning out of range, then indicate that the number is not valid and that they should try again. This program should test for negative numbers, which are not valid. Prompt the user to indicate that the number must be greater than 0 and that they should try again. If the number...

  • QUESTION 12 Write a program that would prompt the user to enter an integer. The program...

    QUESTION 12 Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube = x...

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • Java Write a program that prompt the user to enter a decimal number representing the radius...

    Java Write a program that prompt the user to enter a decimal number representing the radius of a circle. If the number is greater or equal to zero, the program displays the area of the circle. Otherwise, the program displays “negative input”

  • Write a Java program that performs these operations Prompt the user to enter a low height...

    Write a Java program that performs these operations Prompt the user to enter a low height in feet (an integer) Prompt the user to enter a high height in feet (an integer) Prompt the user to enter a low weight in pounds (an integer) Prompt the user to enter a high weight in pounds (an integer) Print a table of Body Mass Index (BMI) for the heights and weights entered, ranging from the low height to the high height (inclusive),...

  • Java prompt the user to enter and display three different variables(an integer, a float and a...

    Java prompt the user to enter and display three different variables(an integer, a float and a double) using the dialog box.

  • This is my python assignment You wrote a program to prompt the user for hours and...

    This is my python assignment You wrote a program to prompt the user for hours and rate per hour to compute gross pay. Also your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Enter Hours: 45 Enter Rate: 10 Pay: 475.0 (475 = 40 * 10 + 5 * 15) Rewrite your pay program using try and except so that your program handles non-numeric input gracefully. Enter Hours: 20 Enter Rate:...

  • (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive...

    (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC) getInputs calculatePay printPay Create...

  • Write a function to have a user enter some number of integers into an array. The...

    Write a function to have a user enter some number of integers into an array. The integer values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integer array and the size of the array are passed into the function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform...

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