Java Math: Exercises, Practice, Solution
Java: Math Exercises [29 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Round Up Integer Division
Write a Java program to round up integer division results.
Click me to see the solution
2. Split Whole and Fractional Parts
Write a Java program to get whole and fractional parts from a double value.
Click me to see the solution
3. Check if Double is Integer
Write a Java program to test if a double number is an integer.
Click me to see the solution
4. Round Float to Specified Decimals
Write a Java program to round a float number to specified decimals.
Click me to see the solution
5. Count Absolute Distinct Values in Array
Write a Java program to count the absolute distinct value in an array.
Click me to see the solution
6. Reverse an Integer
Write a Java program to reverse an integer number.
Click me to see the solution
7. Roman to Integer Conversion
Write a Java program to convert a Roman number to an integer number.
Click me to see the solution
8. Integer to Absolute Value
Write a Java program to convert an integer value to an absolute value.
Click me to see the solution
9. Float to Absolute Value
Write a Java program to convert a floating value to an absolute value.
Click me to see the solution
10. Round Float Value
Write a Java program to accept a float value of a number and return a rounded float value.
Click me to see the solution
11. Check for 15 Condition
Write a Java program to accept two integers and return true if either is 15 or if their sum or difference is 15.
Click me to see the solution
12. Count Primes Below Number
Write a Java program to count the number of prime numbers less than a given positive number.
Click me to see the solution
13. Longest Binary Zero Sequence
Write a Java program to find the length of the longest sequence of zeros in binary representation of an integer.
Click me to see the solution
14. Babylonian Square Root
Write a Java program to find the square root of a number using the Babylonian method.
Click me to see the solution
15. Multiply Without Operators
Write a Java program to multiply two integers without multiplication, division, bitwise operators, and loops.
Click me to see the solution
16. Power Without Multiplication or Division
Write a Java program to calculate power of a number without using multiplication(*) and division(/) operators.
Click me to see the solution
17. Stream Average Calculation
Write a Java program to calculate and print the average (or mean) of the stream of given numbers.
Click me to see the solution
18. Count Numbers Without 7
Write a Java program to count numbers without 7 from 1 to a given number.
Click me to see the solution
19. Generate Magic Square
Write a Java program to generate a magic square of order n (all row, column, and diagonal sums are equal).
From Wikipedia,
In recreational mathematics and combinatorial design, a magic square is a n x n square grid (where n is the number of cells on each side) filled with distinct positive integers in the range 1, 2, ..., n2 such that each cell contains a different integer and the sum of the integers in each row, column and diagonal is equal. The sum is called the magic constant or magic sum of the magic square. A square grid with n cells on each side is said to have order n.
Click me to see the solution
20. Sieve of Eratosthenes for Primes
In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
Write a Java program to print all primes smaller than or equal to any given number.
Click me to see the solution
21. Max Distinct Prime Factors in Range
Write a Java program to find the number with the maximum number of distinct prime factors in a given range.
Click me to see the solution
22. Next Smallest Palindrome
Write a Java program to find the next smallest palindrome.
Click me to see the solution
23. Evaluate Polynomial Efficiently
A fast scheme for evaluating a polynomial such as:
-19+ 7x- 4x2 + 6x3
when
x=3
is to arrange the computation as follows:((((0)x+6)x+(-4))x+7)x+(-19)
Write a Java program to compute the result from the innermost brackets.
Click me to see the solution
24. Calculate Binomial Coefficient
Write a Java program to calculate the Binomial Coefficient of two positive numbers.
Click me to see the solution
25. Taylor Series for e^x
Write a Java program to calculate e raise to the power x using the sum of the first n terms of the Taylor Series.
Click me to see the solution
26. Prime Factors of Number
Write a Java program to print all the prime factors of a given number.
Click me to see the solution
27. Check Fibonacci Number
Write a Java program to check if a given number is a Fibonacci number or not.
Click me to see the solution
28. Excel Column from Number
Write a Java program to find the Excel column name that corresponds to a given column number (integer value).
Click me to see the solution
29. Angle Between Clock Hands
Write a Java program to find the angle between the hour and minute hands.
Click me to see the solution
Java Practice online
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Previous: Java Collection Exercises Home.
Next: Write a Java program to round up the result of integer division.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/math/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics