Java Exercises
Java Exercises
What
are the values of x and y?
2. Assign the value 7 to a variable num1 and 3 to num2. Write a program to calculate their
sum, difference, product, and quotient.
3. Assign 5 to an integer variable a and 5.5 to a double variable b. Then, print the result of
adding them together.
4. Assign the length and width of a rectangle to two variables and calculate its area.
Example: length = 10, width = 5.
5. Assign values to the principal (P), rate of interest (R), and time (T), and calculate the
simple interest using the formula:
Simple Interest=P×R×T100
Example: P = 1000, R = 5, T = 2.
6. Assign a value to a variable Celsius and convert it to Fahrenheit using the formula:
Fahrenheit=(Celsius×9/5)+32
Example: Celsius = 25.
7. Assign the length and width of a rectangle to two variables and calculate its perimeter.
Formula:
Perimeter=2×(length + width)
Example: length = 12, width = 7.
8. Assign three numbers to variables and calculate their average.
Example: num1 = 15, num2 = 25, num3 = 35.
9. Assign a number to a variable and calculate both its square and cube.
Example: number = 4.
Square=number × number
Cube=number × number × number