# Java Math: Exercises, Practice, Solution

## Java: Math Exercises [27 exercises with solution]

[*An editor is available at the bottom of the page to write and execute the scripts.*]

**1.** Write a Java program to round up the result of integer division. Go to the editor

Click me to see the solution

**2.** Write a Java program to get whole and fractional parts from a double value. Go to the editor

Click me to see the solution

**3.** Write a Java program to test if a double number is an integer. Go to the editor

Click me to see the solution

**4.** Write a Java program to round a float number to specified decimals. Go to the editor

Click me to see the solution

**5.** Write a Java program to count the absolute distinct value in an array. Go to the editor

Click me to see the solution

**6.** Write a Java program to reverse an integer number. Go to the editor

Click me to see the solution

**7.** Write a Java program to convert Roman number to an integer number. Go to the editor

Click me to see the solution

**8.** Write a Java program to convert an integer value to absolute value. Go to the editor

Click me to see the solution

**9.** Write a Java program to convert a float value to absolute value. Go to the editor

Click me to see the solution

**10.** Write a Java program to accept a float value of number and return a rounded float value. Go to the editor

Click me to see the solution

**11.** Write a Java program to accept two integers and return true if the either one is 15 or if their sum or difference is 15. Go to the editor

Click me to see the solution

**12.** Write a Java program to count the number of prime numbers less than a given positive number. Go to the editor

Click me to see the solution

**13.** Write a Java program to find the length of the longest sequence of zeros in binary representation of an integer. Go to the editor

Click me to see the solution

**14.** Write a Java program to find the square root of a number using Babylonian method. Go to the editor

Click me to see the solution

**15.** Write a Java program to multiply two integers without using multiplication, division, bitwise operators, and loops. Go to the editor

Click me to see the solution

**16.** Write a Java program to calculate power of a number without using multiplication(*) and division(/) operators. Go to the editor

Click me to see the solution

**17.** Write a Java program to calculate and print average (or mean) of the stream of given numbers. Go to the editor

Click me to see the solution

**18.** Write a Java program to count the numbers without digit 7, from 1 to a given number. Go to the editor

Click me to see the solution

**19.** Write a Java program to generate a magic square of order n (all row, column, and diagonal sums are equal). Go to the editor

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, ..., n^{2} 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.** 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. Go to the editor

Click me to see the solution

**21.** Write a Java program to find the number which has the maximum number of distinct prime factors in a given range. Go to the editor

Click me to see the solution

**22.** Write a Java program to find next smallest palindrome. Go to the editor

Click me to see the solution

**23.** A fast scheme for evaluating a polynomial such as:

-19+ 7x- 4x^{2} + 6x^{3}

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. Go to the editor

Click me to see the solution

**24.** Write a Java program to calculate the Binomial Coefficient of two positive numbers. Go to the editor

Click me to see the solution

**25.** Write a Java program to calculate e raise to the power x using sum of first n terms of Taylor Series. Go to the editor

Click me to see the solution

**26.** Write a Java program to print all prime factors of a given number. Go to the editor

Click me to see the solution

**27.** Write a Java program to check if a given number is Fibonacci number or not. Go to the editor

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.

## Java: Tips of the Day

**everyNth:**

Returns every nth element in an array.

Use IntStream.range().filter() to create a new array that contains every nth element of a given array.

public static int[] everyNth(int[] elements, int nth) { return IntStream.range(0, elements.length) .filter(i -> i % nth == nth - 1) .map(i -> elements[i]) .toArray(); }

Ref: https://bit.ly/3CBWdji

**Exercises: Weekly Top 16 Most Popular Topics**- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation