# 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.** Write a Java program to round up integer division results.

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

**9.** Write a Java program to convert a floating value to an absolute value.

Click me to see the solution

**10.** 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.** 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.** Write a Java program to count the number of prime numbers less than a given positive number.

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.

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

**17.** 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.** Write a Java program to count numbers without 7 from 1 to a given number.

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).

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.

Click me to see the solution

**21.** 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.** Write a Java program to find the next smallest palindrome.

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.

Click me to see the solution

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

Click me to see the solution

**25.** 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.** Write a Java program to print all the prime factors of a given number.

Click me to see the solution

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

Click me to see the solution

**28.** 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.** 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.

## Java: Tips of the Day

**Java Classes**

A Java class is declared with the keywords public class along with a unique class name mirroring its file name. For example, in a file Hello.java in project helloworld:

package helloworld; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * @author seth * A GUI hello world. */ public class Hello { // this is an empty class }

You can declare variables and functions inside a class. In Java, variables within a class are called fields.

Ref: https://red.ht/3sL3sSl

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook