0% found this document useful (0 votes)
53 views6 pages

Convert Meters To Kilometers - Problem Description

java zadaci za vezbu

Uploaded by

Danka Dukanac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

Convert Meters To Kilometers - Problem Description

java zadaci za vezbu

Uploaded by

Danka Dukanac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab: Data Types and Variables

Problems for exercises and homework for the "Programming Fundamentals" course @ SoftUni.
You can check your solutions in Judge.

I. Integer and Real Numbers


1. Convert Meters to Kilometers
You will be given an integer that will be a distance in meters. Write a program that converts meters to kilometers
formatted to the second decimal point.

Examples
Input Output
1852 1.85
798 0.80

2. Pounds to Dollars
Write a program that converts British pounds to US dollars formatted to the 3rd decimal point.
1 British Pound = 1.36 Dollars

Examples
Input Output
80 108.800
39 53.040

3. Exact Sum of Real Numbers


Write a program to enter n numbers and calculate and print their exact sum (without rounding).

Examples
Input Output
3 1000000000000000015
100000000000000000
0
5
10
2 333333333333.30000000003
0.00000000003
333333333333.3

Hints
Use BigDecimal not to lose precision.

© SoftUni Global – softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 1 of 6


II. Data Types and Type Conversion
4. City Info
You will be given 3 lines of input. On the first line, you will be given the name of the city, on the second – the
population, and on the third – the area. Use the correct data types and print the result in the following format:
"City of {city name} has population of {population} and area {area} square km.".

Examples
Input Output
London City of London has population of 9002488 and area 1572 square
9002488 km.
1572
Rome City of Rome has population of 4355725 and area 1285 square
4355725 km.
1285

5. Concat Names
Read two names and a delimiter. Print the names joined by the delimiter.

Examples
Input Output
John John->Smith
Smith
->
Jan Jan<->White
White
<->
Linda Linda=>Terry
Terry
=>

6. Chars to String
Write a program that reads 3 lines of input. On each line, you get a single character. Combine all the characters into
one string and print it on the console.

Examples
Input Output
a abc
b
c
% %2o

© SoftUni Global – softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 2 of 6


2
o
1 15p
5
p

7. Reversed Chars
Write a program that takes 3 lines of characters and prints them in reversed order with a space between them.

Examples
Input Output
A C B A
B
C
1 & L 1
L
&

8. Lower or Upper
Write a program that prints whether a given character is upper-case or lower-case.

Examples
Input Output
L upper-case
f lower-case

9. Centuries to Minutes
Write a program to enter an integer number of centuries and convert it to years, days, hours, and minutes.

Examples
Input Output
1 1 centuries = 100 years = 36524 days = 876581 hours = 52594877 minutes
5 5 centuries = 500 years = 182621 days = 4382906 hours = 262974384 minutes

Hints
 Use appropriate data types to fit the result after each data conversion.
 Assume that a year has 365.2422 days on average (the Tropical year).

Solution
You might help yourself with the code below:

© SoftUni Global – softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 3 of 6


10. Special Numbers
A number is special when its sum of digits is 5, 7, or 11.
Write a program to read an integer n and for all numbers in the range 1…n to print the number and if it is special or
not (True / False).

Examples
Input Output
15 1 -> False
2 -> False
3 -> False
4 -> False
5 -> True
6 -> False
7 -> True
8 -> False
9 -> False
10 -> False
11 -> False
12 -> False
13 -> False
14 -> True
15 -> False
9 1 -> False
2 -> False
3 -> False
4 -> False
5 -> True
6 -> False
7 -> True
8 -> False
9 -> False

© SoftUni Global – softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 4 of 6


Hints
To calculate the sum of digits of given number num, you might repeat the following: sum the last digit (num % 10)
and remove it (sum = sum / 10) until the num reaches 0.

III. Variables
11. Refactor Volume of Pyramid
You are given a working code that finds the volume of a pyramid. However, you should consider that the variables
exceed their optimum span and have improper naming. Also, search for variables that have multiple purposes.

Code
Sample Code
Scanner scanner = new Scanner(System.in);
double dul, sh, V = 0;
System.out.print("Length: ");
dul = Double.parseDouble(scanner.nextLine());
System.out.print("Width: ");
sh = Double.parseDouble(scanner.nextLine());
System.out.print("Height: ");
V = Double.parseDouble(scanner.nextLine());
V = (dul * sh * V) / 3;
System.out.printf("Pyramid Volume: %.2f", V);

Hints
 Reduce the span of the variables by declaring them at the moment they receive a value, not before
 Rename your variables to represent their real purpose (example: "dul" should become length, etc.)
 Search for variables that have multiple purposes. If you find any, introduce a new variable.

12. Refactor Special Numbers


You are given a working code that is a solution to Problem 10. Special Numbers. However, the variables are
improperly named, declared before they are needed, and some of them are used for multiple things. Without using
your previous solution, modify the code so that it is easy to read and understand.

Code
Sample Code
Scanner scanner = new Scanner(System.in);
int how = Integer.parseInt(scanner.nextLine());
int tol = 0;
int hr = 0;
boolean toe = false;
for (int ch = 1; ch <= how; ch++) {
hr = ch;

© SoftUni Global – softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 5 of 6


while (ch > 0) {
tol += ch % 10;
ch = ch / 10;
}
toe = (tol == 5) || (tol == 7) || (tol == 11);
System.out.printf("%d -> %b%n", hr, toe);
tol = 0;
ch = hr;
}

Hints
 Reduce the span of the variables by declaring them at the moment they receive a value, not before
 Rename your variables to represent their real purpose (example: "toe" should become isSpecialNum, etc.)
 Search for variables that have multiple purposes. If you find any, introduce a new variable

© SoftUni Global – softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 6 of 6

You might also like