Java Data Type Exercises: Reads a number and display the square, cube, and fourth power
Java Data Type: Exercise-8 with Solution
Write a Java program that reads a number and display the square, cube, and fourth power.
Test Data
Input the side length value: 15
Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise8 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the side length value: ");
double val = in.nextDouble();
System.out.printf("Square: %12.2f\n", val * val);
System.out.printf("Cube: %14.2f\n", val * val * val);
System.out.printf("Fourth power: %6.2f\n", Math.pow(val, 4));
}
}
Sample Output:
Input the side length value: 15 Square: .2f Cube: .2f Fourth power: 50625.00
Flowchart:
JavaCode Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to display the speed, in meters per second, kilometers per hour and miles per hour.
Next: Write a Java program that accepts two integers and then prints the sum, the difference, the product, the average, the distance (the difference between integer), the maximum (the larger of the two integers), the minimum (smaller of the two integers).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework