Java Data Types Exercises-:
1. Write a Java program to convert temperature from Fahrenheit to Celsius degrees.
Test Data
Input a degree in Fahrenheit: 212
Expected Output:
212.0 degrees Fahrenheit is equal to 100.0 in Celsius
import [Link];
public class Exercise1 {
public static void main(String[] Strings) {
Scanner input = new Scanner([Link]);
[Link]("Input a degree in Fahrenheit: ");
double fahrenheit = [Link]();
double celsius =(( 5 *(fahrenheit - 32.0)) / 9.0);
[Link](fahrenheit + " degree Fahrenheit is equal to " + celsius + " in Celsius");
}
2. Write a Java program that reads a number in inches and converts it to meters.
Note: One inch is 0.0254 meter.
Test Data
Input a value for inch: 1000
Expected Output :
1000.0 inch is 25.4 meters
import [Link];
public class Exercise2 {
public static void main(String[] Strings) {
Scanner input = new Scanner([Link]);
[Link]("Input a value for inch: ");
double inch = [Link]();
double meters = inch * 0.0254;
[Link](inch + " inch is " + meters + " meters");
3. Write a Java program that reads an integer between 0 and 1000 and adds all the digits in the
integer.
Test Data
Input an integer between 0 and 1000: 565
Expected Output :
The sum of all digits in 565 is 16
import [Link];
public class Main {
public static void main(String[] Strings) {
Scanner input = new Scanner([Link]);
[Link]("Input an integer between 0 and 1000: ");
int num = [Link]();
int firstDigit = num % 10;
int remainingNumber = num / 10;
int SecondDigit = remainingNumber % 10;
remainingNumber = remainingNumber / 10;
int thirdDigit = remainingNumber % 10;
remainingNumber = remainingNumber / 10;
int fourthDigit = remainingNumber % 10;
int sum = thirdDigit + SecondDigit + firstDigit + fourthDigit;
[Link]("The sum of all digits in " + num + " is " + sum);
4. Write a Java program to convert minutes into years and days.
Test Data
Input the number of minutes: 3456789
Expected Output :
3456789 minutes is approximately 6 years and 210 days
import [Link];
public class Exercise4 {
public static void main(String[] Strings) {
double minutesInYear = 60 * 24 * 365;
Scanner input = new Scanner([Link]);
[Link]("Input the number of minutes: ");
double min = [Link]();
long years = (long) (min / minutesInYear);
int days = (int) (min / 60 / 24) % 365;
[Link]((int) min + " minutes is approximately " + years + " years and " + days + "
days");
5. Write a Java program that prints the current time in GMT.
Test Data
Input the time zone offset to GMT: 256
Expected Output:
Current time is 23:40:24
import [Link];
public class Exercise5 {
public static void main(String[] Strings) {
Scanner input = new Scanner([Link]);
[Link]("Input the time zone offset to GMT: ");
long timeZoneChange = [Link]();
long totalMilliseconds = [Link]();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = ((totalHours + timeZoneChange) % 24);
[Link]("Current time is " + currentHour + ":" + currentMinute + ":" +
currentSecond);
}
6. Write a Java program to compute the body mass index (BMI).
Test Data
Input weight in pounds: 452
Input height in inches: 72
Expected Output:
Body Mass Index is 61.30159143458721
import [Link];
public class Exercise6 {
public static void main(String[] Strings) {
Scanner input = new Scanner([Link]);
[Link]("Input weight in pounds: ");
double weight = [Link]();
[Link]("Input height in inches: ");
double inches = [Link]();
double BMI = weight * 0.45359237 / (inches * 0.0254 * inches * 0.0254);
[Link]("Body Mass Index is " + BMI+"\n");
7. Write a Java program to take the user for a distance (in meters) and the time taken (as three
numbers: hours, minutes, seconds), and display the speed, in meters per second, kilometers per
hour and miles per hour (hint: 1 mile = 1609 meters).
Test Data
Input distance in meters: 2500
Input hour: 5
Input minutes: 56
Input seconds: 23
Expected Output :
Your speed in meters/second is 0.11691531
Your speed in km/h is 0.42089513
Your speed in miles/h is 0.26158804
import [Link];
public class Exercise7 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
float timeSeconds;
float mps,kph, mph;
[Link]("Input distance in meters: ");
float distance = [Link]();
[Link]("Input hour: ");
float hr = [Link]();
[Link]("Input minutes: ");
float min = [Link]();
[Link]("Input seconds: ");
float sec = [Link]();
timeSeconds = (hr*3600) + (min*60) + sec;
mps = distance / timeSeconds;
kph = ( distance/1000.0f ) / ( timeSeconds/3600.0f );
mph = kph / 1.609f;
[Link]("Your speed in meters/second is "+mps);
[Link]("Your speed in km/h is "+kph);
[Link]("Your speed in miles/h is "+mph);
[Link]();
8. Write a Java program that reads a number and displays the square, cube, and fourth power.
Expected Output:
Square: .2f
Cube: .2f
Fourth power: 50625.00
import [Link];
public class Exercise8 {
public static void main(String[] args)
Scanner in = new Scanner([Link]);
[Link]("Input the side length value: ");
double val = [Link]();
[Link]("Square: %12.2f\n", val * val);
[Link]("Cube: %14.2f\n", val * val * val);
[Link]("Fourth power: %6.2f\n", [Link](val, 4));
9. Write a Java program that accepts two integers from the user and prints the sum, the difference,
the product, the average, the distance (the difference between the integers), the maximum (the
largest of the two integers), and the minimum (the smallest of the two integers).
Test Data
Input 1st integer: 25
Input 2nd integer: 5
Expected Output :
Sum of two integers: 30
Difference of two integers: 20
Product of two integers: 125
Average of two integers: 15.00
Distance of two integers: 20
Max integer: 25
Min integer: 5
import [Link];
public class Exercise9 {
public static void main(String[] args)
Scanner in = new Scanner([Link]);
[Link]("Input 1st integer: ");
int firstInt = [Link]();
[Link]("Input 2nd integer: ");
int secondInt = [Link]();
[Link]("Sum of two integers: %d%n", firstInt + secondInt);
[Link]("Difference of two integers: %d%n", firstInt - secondInt);
[Link]("Product of two integers: %d%n", firstInt * secondInt);
[Link]("Average of two integers: %.2f%n", (double) (firstInt + secondInt) / 2);
[Link]("Distance of two integers: %d%n", [Link](firstInt - secondInt));
[Link]("Max integer: %d%n", [Link](firstInt, secondInt));
[Link]("Min integer: %d%n", [Link](firstInt, secondInt));