0% found this document useful (0 votes)
9 views

Java Foundations Lesson 2 Data Types and Type Conversion Exercises

This document contains examples of 8 problems involving data types and variables in Java, such as converting between meters and kilometers, pounds to dollars, summing real numbers, and calculating centuries in other units of time. It also provides problems for checking if a number is special based on its digit sum, reversing the order of characters, concatenating names with a delimiter, and next steps for joining the SoftUni learning community.

Uploaded by

Lusper Mhango
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Foundations Lesson 2 Data Types and Type Conversion Exercises

This document contains examples of 8 problems involving data types and variables in Java, such as converting between meters and kilometers, pounds to dollars, summing real numbers, and calculating centuries in other units of time. It also provides problems for checking if a number is special based on its digit sum, reversing the order of characters, concatenating names with a delimiter, and next steps for joining the SoftUni learning community.

Uploaded by

Lusper Mhango
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Foundations

Exercises for: Data


Types and Variables,
Boolean, Integer, Char,
String, Type
Conversion
2

Problem: Convert Meters to Kilometres


▪ Write a program that converts meters to kilometers formatted
to the second decimal point
▪ Examples: 1852 1.85 798 0.80

Scanner scanner = new Scanner(System.in);

int meters = Integer.parseInt(scanner.nextLine());


double kilometers = meters / 1000.0;
System.out.printf("%.2f", kilometers);
3

Problem: Pound to Dollars


▪ Write a program that converts British pounds to US dollars
formatted to 3rd decimal point
▪ 1 British Pound = 1.31 Dollars

80 104.800 39 51.090

double num = Double.parseDouble(scanner.nextLine());


double result = num * 1.31;
System.out.printf("%.3f", result);
4

Problem: Exact Sum of Real Numbers


▪ Write a program to enter n numbers and print their exact sum:

2
1000000000000000000 1000000000000000005
5

2
0.00000000003 333333333333.30000000003
333333333333.3
5

Problem: Centuries to Minutes


▪ Write program to enter an integer number of centuries and
convert it to years, days, hours and minutes

1 centuries = 100 years = 36524 days


1
= 876576 hours = 52594560 minutes

5 centuries = 500 years = 182621 days


5
= 4382904 hours = 262974240 minutes
The output is
on one row
6

Problem: Special Numbers


▪ A number is special when its sum of digits is 5, 7 or 11
▪ For all numbers 1…n print the number and if it is special

1 -> False 8 -> False 15 -> False


2 -> False 9 -> False 16 -> True
3 -> False 10 -> False 17 -> False
20 4 -> False 11 -> False 18 -> False
5 -> True 12 -> False 19 -> False
6 -> False 13 -> False 20 -> False
7 -> True 14 -> True
7

Problem: Reversed Chars


▪ Write a program that takes 3 lines of characters and prints them in
reversed order with a space between them
▪ Examples:

A 1
B C B A L & L 1
C &
8

Problem: Concat Names


▪ Read first and last name and delimiter
▪ Print the first and last name joined by the delimiter
John Linda
Smith John->Smith Terry Linda=>Terry
-> =>

Jan Lee
White Jan<->White Lewis Lee---Lewis
<-> ---
Next Steps

▪▪ Join
… the SoftUni "Learn To Code" Community
▪ …
▪ …https://softuni.org
▪ Access the Free Coding Lessons
▪ Get Help from the Mentors
▪ Meet the Other Learners

You might also like