Write a
- Use scanner to read a floating-point value x.
- Compute 1.0 I x and store the result in y.
- Display x, y, and the product of x and y.
- Subtract 1 from the product of x and y and display the result.
Try your program with values of x that range from 2e−11 to 2ell. What can you conclude?
Program Plan:
- • Include the required header files.
- • Define main function.
- ○ Create an object for scanner.
- ○ Get the floating-point value from the user.
- ○ Calculate the “y” value.
- ○ Display “x”, “y” and product of “x” and “y”.
- ○ Subtract “1” from product of “x” and “y” and then display the result.
The program is used to read a floating point value then compute “y” and display “x”, “y”, product of “x” and “y” and also subtract “1” from product of “x” and “y” then display the result as follows:
Explanation of Solution
Program:
//include required header files
import java.util.Scanner;
//definition of "Approximation" class
public class Approximation
{
//definition of main method
public static void main(String[] args)
{
//create an object for scanner
Scanner keyboard = new Scanner(System.in);
//declare the required variable
float x = 0, y;
//get the input from the user
System.out.println("Enter a floating point value.");
x = keyboard.nextFloat();
//calculate "y" value
y = (float) (1.0/x);
//display "x" and "y" value
System.out.println("x value is: "+ x);
System.out.println("y value is: "+ y);
//display product of "x" and "y"
System.out.println("Product of x and y is: "+ (x*y));
/*subtract 1 from product of "x" and "y" and display the result*/
System.out.println("The difference of x*y and 1 is " + ((x*y) - 1.0));
}
}
Conclusion:
The “x” depends on user but the product of “x” and “y” will always give “1” and difference of “x * y” and “1” is always “0” because, “1” is divided by “y” and then “y” is multiplied with the “x”. So there is no change in product of “x” and “y” and difference of “x * y” and “1”.
Output:
Enter a floating point value.
2.5
x value is: 2.5
y value is: 0.4
Product of x and y is: 1.0
The difference of x*y and 1 is 0.0
Want to see more full solutions like this?
Chapter 2 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Introduction To Programming Using Visual Basic (11th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (8th Edition)
Concepts Of Programming Languages
- Write a program that reads two integers, a and b, from the user. Where a is equal to 17 and b is equal to 4. The program should compute and display the following: i. Sum of a and b. ii. Difference when b is subtracted from a. ii. Result of a. iv. Remainder when a is divided by b.arrow_forward. Write a program that generates and prints 50 random integers, each between 3 and 6. .Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes x". .Write a program that generates a random number between 1 and 10 and prints your name that many times. .Write a program that generates a random decimal number between 1 and 10 with two decimal places of accuracy. Examples are 1.23, 3.45, 9.80, and 5.00.arrow_forwardA number is a palindrome if its reversal is the same as itself. An even palindrome number is a number which is even and also a palindrome. Write a program that displays the first 50 even palindrome numbers. Display 5 numbers per line and align the numbers properly.arrow_forward
- Please answer it in Python https://www.codingame.com/ide/puzzle/temperatures Write a program that prints the temperature closest to 0 among input data. If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5). Your program must read the data from the standard input and write the result on the standard output. Input Line 1: N, the number of temperatures to analyze Line 2: A string with the N temperatures expressed as integers ranging from -273 to 5526 Output Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0. Constraints 0 ≤ N < 10000arrow_forwardUse Java Programming Given the input n, make a Z pattern using the z character. However, you can only form the pattern "z" if n is odd. If n is even, get the next off number from it (6 to 7) and from that odd number, form the pattern "z"arrow_forwardWrite a program that takes ten integers from the input and displays them inversely in the output.arrow_forward
- Write a program that reads an integer and displays all its smallest factors, also known as prime factors. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5arrow_forwardCode the follow.arrow_forwardWrite a program that reads some integers between 1 and 100 and counts the occurrences of each.arrow_forward
- Task Write a MASM program that will resemble a digital clock. The clock will display hh:mm:ss (hour, minute and second format; 2 digits for each), i.e., 03:10:05. The clock will take an input from the user (0 to 9) which will be the starting time of the clock. Say the input is 6, so the clock start time is 00:00:06. 00:00:59. You increment the second by one, i.e., 00:00:06, 00:00:07, Then you need to change the minute to 01 and second to 00, i.e., 00:01:00. Similarly, change the time for hour e.g., 00:59:59 will change to 01:00:00. It is a 12 hour clock. After 11:59:59, the clock will change to 00:00:00. You may like to run the attached Calcul.exe to understand the expectation. ● Commenting Most of the lines of the program code should be commented (short comment is fine) so that your code could be well understood.arrow_forwardWrite a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge. Not use Boolean operationarrow_forwardRead floating-point numbers from input until a floating-point number is read that is not in the range -45.0 to 15.0, both exclusive. For each floating-point number read that is in the given range, output the floating-point number followed by " is accepted". Then, output the floating-point number read that causes the reading to stop followed by " is rejected". End each output with a newline. Ex: If the input is -41.6 -37.8 -35.5 -38.4 -42.3 -49.5 -36.9 -41.1, then the output is: -41.6 is accepted -37.8 is accepted -35.5 is accepted -38.4 is accepted -42.3 is accepted -49.5 is rejected 1 #include 2 using namespace std; 3 4 int main() { 5 167899 10} double dataValue; /* Your code goes here */ return 0;arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning