- 0shares
- Facebook0
- Twitter0
- Google+0
- Pinterest0
- LinkedIn0

## Java Programs

In this section we will be explaining the commonly asked Java programs that can be related to arrays in Java, control structure in Java, strings in Java or the object oriented programming in Java.

Some of the programs are discussed below:

### Fibonacci series:

In Fibonacci series each next number is the sum of the two previous numbers. The Fibonacci series is written in the following way:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34…

In the Fibonacci series the first two numbers are 0 and 1. In Java we can write the Fibonacci in two ways. That is Fibonacci series with and without recursion.

### Fibonacci Series in Java without using recursion:

Consider the following example in which we have implemented Fibonacci series without using recursion:

**CODE:**

class ExampleFibonacci {

public static void main (string [] args) {

int a = 0;

int b = 1;

int c, j;

int count = 10;

system. out. println (a+ “ ” +b);

for (j = 2; j <= count; ++ j) {

c = a + b;

system. out. println (“ ” + c);

a = b;

b = c;

}

}

}

**OUTPUT:**

0 1 1 2 3 5 8 13 21 34

In the above example a class is defined named ExampleFibonacci. Then we declared 5 variables a, b, c, j, and count. The variables ‘a’ and ‘b’ are initialized at 0 and 1 respectively. And the variable ‘count’ is initialized at 10. The variable ‘j’ is the counter variable for the loop. Then the variables ‘a’ and ‘b’ are printed to print the first two numbers of the series. The loop is started from 2 and will terminate when the counter variable becomes equal to the variable ‘count’.

The sum of variables ‘a’ and ‘b’ are is stored in variable ‘c’ and c is printed. The variables ‘a’ and ‘b’ are the previous values and ‘a’ is assigned the value of ‘b’ and ‘b’ is assigned the value of ‘c’ and in each iteration sum of these values are stored in ‘c’ and c is printed. In this way the Fibonacci series will be printed without using recursion.

### Fibonacci series using recursion in Java:

Consider the following example in which we have used recursion to implement the Fibonacci series:

**CODE:**

class ExampleFibonacci {

static int a = 0, b = 1, c = 0;

static void Fibonacci (int count) {

if (count > 0) {

c = a + b;

a = b;

b = c;

system. out. print (“ ” +c);

Fibonacci (count-1);

}

}

public static void main (string [] args) {

int count = 10;

system. out. print (a+ “ ” +b);

Fibonacci (count-2);

}

}

**OUTPUT:**

0 1 1 2 3 5 8 13 21 34

A recursive function is that which is called in its body. In the above example we used the recursive function for the implementation of Fibonacci series.

### Prime Number Program in Java:

A prime number is a number that is divided by itself or by 1 and is greater than 1. For example prime numbers are 2, 3, 5, 7, 11, 13 etc. all are prime numbers.

The numbers 0 and 1 are not prime numbers. The only even number that is a prime number is 2 and all the other even numbers can be divided by 2 and hence they are not prime numbers.

Consider the following example in which we have checked a number that if it is a prime number or not:

**CODE:**

class ExamplePrime {

public static void main (string [] args) {

int j, flag = 1;

int n = 13;

for (j = 2; j <= n; j++) {

if (n % j == 0) {

flag = 0;

break;

}

}

if (flag == 1) {

system. out. println (“Number is a prime number”);

}

else {

system. out. println (“Number is not a prime number”);

}

}

}

**OUTPUT:**

Number is a prime number.

In the above example, three variables are declared ‘j’, the counter variable, ‘n’ the variable whose value will be checked and another variable named flag. We used the variable ‘flag’ to increment its value if the number is not divisible by 2 and the message will be printed that number is prime if no increment is made on the flag that is number can be divisible by 2 then message will be printed that the number is not prime.

### Palindrome Program in Java:

A palindrome is a number that is same when it is reversed for example 151 is a palindrome number as when it is reversed it will be the same.

### Palindrome Number algorithm:

Consider the following steps in which the algorithm to check if the number is palindrome or not is given:

- Number to be checked to be palindrome
- Declare a temporary variable to save this number
- Now reverse the number
- Then compare the number that was saved in the temporary variable with the reversed number
- If these numbers are same print that the number is palindrome
- Else print that number is not palindrome

Consider the following code in java programming language in which we have checked a number to be palindrome or not:

**CODE:**

class ExamplePalindrome {

public static void main (string [] args) {

int b, temp;

int sum = 0;

int n = 151;

temp = n;

while (n > 0) {

b = n % 10;

sum = (sum * 10) + b;

n = n / 10;

}

if (temp == sum) {

system. out. println (“The number is Palindrome”);

}

else {

system. out. println (“The number is not Palindrome”);

}

}

}

**OUTPUT:**

The number is Palindrome

In the above example 4 variables are declared. The variable ‘n’ is the number to be checked and is stored in the temporary variable ‘temp’. Then in the while loop we reversed the number by finding the modulus of the number first and then finding the sum. Then the variable ‘sum’ is matched with temporary variable in which the number to be checked to be palindrome was stored. If the two numbers match then a message will be printed that “The number is Palindrome”, if the variables ‘temp’ and ‘sum’ do not match then the message will be printed that “The number is not Palindrome”.

### Factorial Number:

Suppose we have a non negative integer n, then the factorial of the number n will be the product of all the positive integers that are less than and equal to n. The factorial of n is denoted by “n!” For example the factorial of the number 3 is “3!” and is equal to

3*2*1 = 6

And the factorial of the number 4 will be equal to

4*3*2*1 = 24

We can also call “4!” as: “4 bang” or “4 shriek”. In Mathematics the factorial of any number is used in Permutations and Combinations.

The factorial of any number in Java programming language can be written in two ways:

- Factorial program using loop
- Factorial program using recursion

### Factorial program using loop in Java:

Consider the following example in which we have used the ‘for’ loop to find the factorial of a number in Java programming language:

**CODE:**

class ExampleFactorial {

public static void main (string [] args) {

int j, fact = 1;

int n = 4;

for (j= 1; j <= n; j++) {

fact = fact * j;

}

system. out. println (“Factorial of entered number is:” + fact);

}

}

**OUTPUT:**

Factorial of entered number is 24

In the above example, we declared three variables the variable ‘j’ is the counter variable and the variable ‘fact’ is the variable in which the factorial of the number will be stored. The variable ‘fact’ is initialized at 1. The variable ‘n’ is used to store the number whose factorial is to be calculated. The loop is terminated when the counter variable becomes equal to the value or variable ‘n’. In this way the variable ‘fact’ is multiplied with the counter variable ‘j’ and the result is stored in variable ‘fact’. Then the factorial of the entered number is printed.

### Factorial Program using recursion in Java:

Consider the following example in which we have used function recursion to find the factorial of a number in Java programming language. By recursion of function we mean that we are calling the function in its body.

**CODE:**

class ExampleFactorial {

static int fact (int n) {

if (n == 0) {

return 1;

}

else {

return (n * fact ( n-1));

}

public static void main (string [] args) {

int j, factorial = 1;

int x = 4;

factorial = fact (x);

system. out. println (“factorial of entered number is:” + factorial);

}

}

**OUTPUT:**

Factorial of entered number is 24

In the above example, we defined a function to calculate the factorial of the number and the function calls itself and hence we did not need to use any looping construct in our program. As the factorial of the number ‘0’ is 1, therefore, we set a condition that when the number is 0 then compiler should return 1 and in the else condition we returned the calculated factorial of the number.

The statement (n * fact (n-1)) calculates the factorial, in which function calls itself by passing the argument (n-1) and multiplies it with the number ‘n’. In the main function three variables are declared. The variable ‘x’ is the number whose value is to be calculated and is passed to the function. The variable ‘factorial’ is used to store the result. Then we used the print statement to print the factorial that was stored in the variable ‘factorial’.

### Armstrong Number in Java:

In mathematics an Armstrong number is a number which is equal to the sum of the cubes of its digits. That is an Armstrong is considered as a three digit number in which the sum of the cubes of its digit will be equal to the number itself. Consider the example 153, 370, 371, etc.

Now consider the following example in which we have demonstrated that how 371 can be an Armstrong number:

371 = (3*3*3) + (7*7*7) + (1*1*1)

In the above example (3*3*3) is equal to 27, (7*7*7) is equal to 343 and (1*1*1) is equal to 1. When we add these numbers we will get 371 as:

27 + 343 + 1 = 371

Consider the following program in which we are going to check a number that if it is an Armstrong number or not:

class ExampleArmstrong {

public static void main (string [] args) {

int b = 0, c, temp;

int n = 371;

temp = n;

while (n > 0) {

c = n % 10;

n = n/10;

b = b + (c*c*c);

}

if (temp == b) {

system. out. println (“The number is an Armstrong number”);

}

else {

system. out. println (“The number is not an Armstrong number”);

}

}

}

**OUTPUT:**

The number is an Armstrong number

In the above example the number to be checked is entered and is stored in the variable ‘n’ then we copied this number to a temporary variable that is ‘temp’. In the ‘while’ loop, different statements are used to check the number to be Armstrong and if the obtained number becomes equal to ‘temp’ then a message will be printed that the number is Armstrong otherwise the message will be printed that the number is not an Armstrong number.

### Bubble Sort in Java:

In Java programming language, the technique bubble sort can be sued to sort an array of elements. The bubble sorting technique is the simplest technique to sort the elements of the array. In Java programming language or any other programming language, the bubble sorting technique implies that the current element of the array will be compared with the next element of the array and if the current element of the array is greater than the next element then it will be swapped. In bubble sorting technique the array of elements is traversed from the first element to the last element.

Consider the following example in which we have implemented the bubble sorting technique to sort an array of elements.

**CODE:**

public class ExampleBubbleSort {

static void BubbleSorting (int [] arr) {

int n = arr. length;

int temp = 0;

for (int j = 0; j < n; j++) {

for (int i = 1; i < (n-j); i++) {

if (arr[i-1] > arr [i]) {

temp = arr [i-1];

arr [i – 1] = arr[i];

arr [i] = temp;

}

}

}

}

public static void main (string [] args) {

int arr [] = {2, 4, 56, 43, 7, 82, 1};

system. out. println (“Array before bubble sorting”);

for (int j = 0; j < arr. length; j++) {

system. out. print (arr [j] + “ ”);

}

system. out. println ();

BubbleSorting (arr);

system. out. println (“Array after bubble sorting”);

for (int j = 0; j < arr.length; j++) {

system. out. print (arr[j] + “ ”);

}

}

}

**OUTPUT:**

Array before bubble sorting

2 4 56 43 7 82 1

Array after bubble sorting

1 2 4 7 43 56 82

In the above example, nested loop is used to swap the elements of the array. Each current element of the array is compared with the next element of the array and if the next element is smaller than the current element of the array then swapping of elements is performed.

### Selection Sort in Java:

Selection sorting can also be performed in Java programming language. In selection sorting, the lowest element or the smallest element of the array is searched and is then arranged to appropriate location in the array. The current element of the array is swapped with the lowest or smallest element of the array.

Consider the following program in which we have used the selection sorting technique to sort an array of elements:

**CODE:**

public class ExampleSelectionSort {

public static void SelectionSort (int [] arr) {

for (int j = 0; j < arr. length; j++) {

int index = j;

for (int i = j + 1; i < arr. length; i++) {

if (arr [i] < arr [index]) {

index = i;

}

}

int lowerNumber = arr [index];

arr [index] = arr [j];

arr [j] = lowerNumber;

}

}

public static void main (string a []) {

int [] array = {2, 4, 56, 43, 7, 82, 1};

system. out. println (“Array before selection sorting”);

for (int j: array) {

system. out. print (j + “ ”);

}

system. out. println ();

selectionSort (array);

system. out. println (“Array after selection sorting”);

for (int j: array) {

system. out. print (j + “ ”);

}

}

}

**OUTPUT:**

Array before selection sorting

2 4 56 43 7 82 1

Array after selection sorting

1 2 4 7 43 56 82

In the above example, selection sorting is used to sort the elements of the array. At first we searched for the lowest or smallest element in the array. And then we swapped the lowest element of the array with the current element of array. In this way the entire array will be sorted.

### Insertion Sort in Java:

In java programming language we can perform insertion sorting to sort the elements of array. Insertion sorting takes a lot of time for sorting large number of elements. In the insertion sorting shifting is performed that is an element is compared with other element to its left and if it is smaller than element on left side then it will be shifted. Insertion sorting is performed from the right side of the array.

Consider the following program in which we have sorted an array using insertion sorting technique:

**CODE:**

public class ExampleInsertionSort {

public static void InsertionSorting (int array []) {

int n = array. length;

for (int j = 1; j <= n; j++) {

int key = array [j];

int i = j – 1;

while (( i > -1) && (array [i] > key)) {

array [i +1] = key;

}

}

public static void main (string a []) {

int [] arr = {2, 4, 56, 43, 7, 82, 1};

system. out. println (“Array before insertion sorting”);

for (int i: arr) {

system. out. print (i + “ ”);

system. out. println ();

InsertionSorting (arr);

system. out. println (“Array after insertion sorting”);

for (int j: arr) {

system. out. print (i+ “ ”);

}

}

}

**OUTPUT:**

Array before insertion sorting

2 4 56 43 7 82 1

Array after insertion sorting

1 2 4 7 43 56 82