## Java Fibonacci Number Generator

Before starting coding our Java Fibonacci number generator let’s recap what it is. Fibonacci series are made out of terms defined as: a1 = 1; a2 = 2; a3 = a1 + a2; ………………… an = an-2 + an-1   Java code: Output: 1 1 2 3 5 8 13 21 34 55 89 144 … Read more

## Java – Convert to any base (Binary, Octal, Hex)

Converting between decimal and an arbitrary base x is done by evenly dividing the decimal number to the base x until the division quotient is zero.   Example: converting 23 (decimal) to binary (base 2): 23 / 2 = 11, remainder 1 11 / 2 = 5, remainder 1 5 / 2 = 2, remainder 1 2 / 2 … Read more

## Java – Check if number is prime

To check if a number is prime means generating all natural numbers greater or equal to 2 and smaller or equal to the square root of the number we want to check. Then we verify if one of these numbers can evenly divide the number we want to check. If this is true, then the … Read more

## Java – Least common multiple

Finding the least common multiple out of two numbers is done using the greatest common divisor. Exemple: Number 27 and 36. Their greatest common divisor (the largest number that can divide both evenly) is 9. 27 / 9 = 3 and 36 / 9 = 4. So the least common multiple (smallest number that can … Read more

## Java – Insertion Sort Algorithm

For this method we need two arrays: source and destination. Elements of the source array will be inserted in the corresponding position, so that in the destination array the order relation will be fulfilled.   Step 1: We copy the first element of the source array into the first position of the destination array. Step … Read more

## Java – Greatest common divisor

The greatest number that can divide evenly a set of two numbers can be found using Euclid’s algorithm: This works by continuously applying modulo until we get the remainder zero: Let’s take two numbers, 51 and 15. We do the modulo operation on them: 51 % 15 = 6 (because 51 / 15 = 3 … Read more

## Java – Extract digits from number

The algorithm for extracting digits from a number starts with the least significative one. For example let’s consider the number: 156.We first apply the modulo 10 on it to and get the remainder 6 (because dividing 156 ‘perfectly’ by 10 gives us the number 15 and a remainder: 6). We now have our first digit. … Read more

## Maximum value of a set of variables

How to find the maximum value of set of entered variables in Java. The code snippet below allows you to input integers and it always notifies you of the greatest value you entered. To stop it press CTRL + Z so scanner will no longer have a “next” value, and break out of the while loop. To … Read more

## Swap two numbers in Java

Swapping numbers in Java using two and three variables. Variant one: Swapping by using only the two variables works the following way:   Proof: a = 3 – 5 = -2; b = -2 + 5 = 3; a = 3 – (-2) = 5; Swapping by using a third variable is even simpler We … Read more