Category Archives: Java Programs

Java Programs

Java Program to sort the elements of an array in ascending order

In this tutorial, we will learn to create a Java Program to sort the elements of an array in ascending order using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Algorithm

  • STEP 1: START
  • 2: Ask user to enter size of an array.
  • 3:Then take elements of an  array
  • 4: iterate each value through loop.
  • 5 for (int i = 0; i < number; i++)
  •             {
  •            for (int j = i + 1; j < number; j++)
  •            {
  •            if(arr[i]>arr[j]) then
    temp = arr[i]
    arr[i]=arr[j]
    arr[j]=temp
  •            }
  •           }
  • STEP 6:  PRINT “Elements of array sorted in ascending order”
  • STEP 7: for (int i = 0; i < number – 1; i++)
  •              {
  •             System.out.print(arr[i] + “,”);
  •               }
  •              System.out.print(arr[number – 1]);
  • STEP 8: END

Java Program to sort the elements of an array in ascending order

In this program we will take size of an array  and sort the elements of an array using nested for loop. We would first declared and initialized the required variables. Next, we will sort the elements in an array. Lets have a look at the code.

Output

Sorted elements in an array.

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

Elements will be sorted in a way that the smallest element will show on extreme left which in this case is 1. The largest element will show at extreme right which in this case is 63.As shown in the image below.

Steps to change elements.

  1. Compare elements with each other.
  2. Use nested for loop to keep track.
  3. Swap the elements if the first element is greater than the second element.
  4. this process followed till all the elements in array are sorted.

and finally we will print the sorted elements.

Java Program to right rotate the elements of an array

In this tutorial, we will learn to create a Java Program to right rotate the elements of an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Rotate an array right means?

Array is said to be right rotated if elements in an array are moved to its right >> by one(1) position.

 

Example:

Array=> [1, 2, 3, 4, 5].
Rotate Rights=> [2, 3, 4, 5, 1].

Java Program to right rotate the elements of an array

In this program we will shift right element in an array using  for loop. We would first declared and initialized the required variables. Next, we will shift element of an array. Lets have a look at the code.

Output

Right rotated array[]

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values
  • rotby=  it will hold rotation position.
  • i       = for iteration
  • last = it will hold length of an array[].

After declaring variables we initiate values in  an array[]

after that we will call rightRotate(a,rotby) where we pass two arguments one array and another is rotate by value i.e how many time rotate the array to its right.

In an array, if rotby is “1” then, elements of an array moved to its right by one position it mean first element of array take second(2) position, the second element will be moved to the third position and so on. as shown down below->

Array=> [1, 2, 3, 4, 5].
Rotate Rights=> [2, 3, 4, 5, 1].

Finally print right rotated array .

Java Program to print sum of all the items of the array

In this tutorial, we will learn to create a Java Program to print sum of all the items of the array  using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is An array?

The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single and multidimensional.

Program description to Find Sum of all Elements in an Array.

In this program, we will first take the input array size and the elements from the user. Then we will add all the elements of the array using the for loop. At last, we will print that sum of elements of the array using the print function.

Output:-

In the above program, we have first initialized the required variable.

  • sum = it will hold the integer value for the sum of numbers.
  • arry[] = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • sz = it will hold the integer value of the input.

Input the number of elements and the elements of the array from the user.

Program Logic Code to find the sum of an array.

Printing output sum of the array elements.

Java Program to print number of elements present in an array

In this tutorial, we will learn to create a Java to Program Find 2nd Smallest Number in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Algorithm

  • STEP 1: start
  • 2: declare and initiate array = {1,2,3,4,5}.
  • 3: Find length using length function ex=> a.length
  • 4: exit.

Java Program to print number of elements present in an array

In this program we will find number of element in an array using length function. We would first declared and initialized the required variables. Next, we will find length of an array element in an array. Lets have a look at the code.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values.

After declaring variables we initiate values in  an array[].

In this our program, we will count  the element and print number of elements present in an array.

As we can see that number of elements present in an array can be found by calculating the length of given array. We can clearly see that length of a function is 5 so here we will use length function to calculate length.

So Length in above array is 5. Then number of elements present in the array are 5.

Finally we will print the result as shown in image above..

Java Program to print smallest element in an array

In this tutorial, we will learn to create a Java Program to print smallest element in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java Program to print smallest element in an array

In this program we will find smallest element in an array using nested for loop. We would first declared and initialized the required variables. Next, we will find smallest number in an array. Lets have a look at the code.

Output

Smallest in an array

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

with the help of length function we  will find the length of an array .

after that with the help or nested for loop we will find smallest element in array as shown in image below

Smallest in an array

  • Here first we assign first value of an array to variable temp.
  • Compare the first two elements of the array.
  • If the value of first element is greater than the second exchange them.
  • Then, compare 2 value and 3 value elements if the second element value is greater than the 3 value swap them.
  • Repeat this till the end of the array[].
  • and finally print smallest in an array.

Java Program to print largest element in an array

In this tutorial, we will learn to create a Java Program to print largest element in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java Program to print largest element in an array

In this program we will find smallest element in an array using nested for loop. We would first declared and initialized the required variables. Next, we will find smallest number in an array. Lets have a look at the code.

Output

Largest in an array

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

with the help of length function we  will find the length of an array .

after that with the help or nested for loop we will find smallest element in array as shown in image below

Largest in an array

  • Here first we assign first value of an array to variable temp.
  • Compare the first two elements of the array.
  • If the value of first element is smaller than the second exchange them.
  • Then, compare 2 value and 3 value elements if the second element value is smaller than the 3 value swap them.
  • Repeat this till the end of the array[].
  • and finally print largest in an array.

Java Program to print elements of an array present on odd position

In this tutorial, we will learn to create a Java Program to print elements of an array present on odd position using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Odd even Place in array ?

Odd-Even positions in an array are those element present within even index and for odd present in odd index are called odd even place as shown in the image below. So element on 1,3,5 are odd index value which is => 20,40 and 60 are odd index values.

Java Program to print elements of an array present on odd position

In this program we will print element in present in odd position within the array using for loop. We would first declared and initialized the required variables. Next, we will find odd place  element in an array. Lets have a look at the code.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values
  • i = for iteration.

After declaring variables we initiate values in  an array[]

Algorithm

  • 1: Start.
  • 2: Initialize array with vales a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
  • 3: Simply print “elements of given array present at odd positions”.
  • 4: Repeat  this step for(i=0; i< arr.length; i= i+2) unitll we  reach at last position of an array.
  • 5: Print a[i].
  •  6: End.

with the help of for loop traverse through the array and printing elements of an array by incrementing value of i by i+2 till the last element of the array.

Java Program to print elements of an array present on even position

In this tutorial, we will learn to create a Java Program to print elements of an array present on even position using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Even-Odd Place in array ?

Odd-Even positions in an array are those element present within even index and for odd present in odd index are called odd even place as shown in the image below. So element on 1,3,5 are even index value which is => 10,30 and 50 are even index values.

Java Program to print elements of an array present on even position

In this program we will print element in present in even position within the array using for loop. We would first declared and initialized the required variables. Next, we will find even place  element in an array. Lets have a look at the code.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values
  • i = for iteration.

After declaring variables we initiate values in  an array[]

Algorithm

  • 1: Start.
  • 2: Initialize array with vales a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
  • 3: Simply print “elements of given array present at even positions”.
  • 4: Repeat  this step for(i=1; i< arr.length; i= i+2) unitll we  reach at last position of an array.
  • 5: Print a[i].
  •  6: End.

with the help of for loop traverse through the array and printing elements of an array by incrementing value of i by i+2 till the last element of the array.

Java Program to print elements of an array in reverse order

Java Program to print elements of an array in reverse order

In this tutorial, we will learn to create a Java program that will print elements of an array in reverse order using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Reverse Order:-

In reverse order, we first take an array or declare an array, then reverse that array and print it.

Example:-

Array:- {1, 2, 3, 4, 5}

Reversed Array be:-{5, 4, 3, 2, 1} 

Program description to print elements of an array in reverse order.

In this program, First, we will first take the size of the array from the user. Then we will reverse the array using the for-loop and store it in a new variable. Then we will print the reverse of the original array to the user.

With the help of this program, we can print elements of an array in reverse order.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • array[] = it will hold the integer value.
  • i = it will hold the integer value.
  • j = it will hold the integer value.
  • size = it will hold the integer value.

Taking the size of the array and the elements.

Program Code to Reverse an Array.

Printing reversed array to the user.

Java Program to print elements of an array

Java Program to print elements of an array

In this tutorial, we will learn to create a Java program that will Print Elements in an Array using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

What is an array?

An array in a programming language is a collection of a similar set of entities. It means one variable can store multiple values in memory with different indices.

Algorithm:-

Printing the Elements in an Array.

In this program, First, we will first take the size of the array from the user. Then we will take the elements of the array from the user. After that, we will use a for loop to print the elements of the array.

With the help of this program, we can be print the elements of the array.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • no = it will hold the integer value.
  • i = it will hold the integer value.

Taking the size of the array and the elements.

Printing output elements of the array.

Java Program to print the duplicate elements of an array

Java Program to print the duplicate elements of an array

In this tutorial, we will learn to create a Java program that will print the duplicate elements of an array using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

What is an array?

An array in a programming language is a collection of a similar set of entities. It means one variable can store multiple values in memory with different indices.

Algorithm:-

Printing the duplicate Elements in an Array.

In this program, First, we will first take the size of the array from the user. Then we will take the elements of the array from the user. After that, we will use the conditional statements and the loops to check the duplicate elements of the array. Then we will be using a for loop to print the duplicate elements of the array.

With the help of this program, we can be print the duplicate elements of the array.

Program Code:-

 

Output:-

In the above program, we have first initialized the required variable.

  • no = it will hold the integer value.
  • i = it will hold the integer value.
  • j = it will hold the integer value.
  • flag = it will hold the integer value.

Taking the size of the array and the elements.

Finding the duplicate elements of the array.

Printing output duplicate elements of the array.

Java Program to left rotate the elements of an array

In this tutorial, we will learn to create a Java Program to left rotate the elements of an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Rotate an array left, means?

The array is said to be left rotated if elements in an array are moved to its left >> by one(1) position.

Example:

Array⇾ [1, 2, 3, 4, 5].
Rotate left ⇾ [5,1,2,3,4].

Java Program to left rotate the elements of an array

In this program, we will shift the left element in an array using for loop. We would first declare and initialized the required variables. Next, we will shift elements of an array. Let’s have a look at the code.

Output

Left  rotated array[]

In the above program, we have first declared and initialized a set of variables required in the program.

  • arr[]= it will hold array values
  • first =  it will hold rotation position.
  • i       = for iteration of the program.
  • n = it will hold the integer value for left side.

After declaring variables we initiate values in  an array[]

After that, we will call left Rotate(n , first ) where we pass two arguments one array and another is rotated by value i.e. how many times rotate the array to its Left.

In an array, if Lotby is “1” then, elements of an array are moved to its Left by one position it means the first element of the array takes the second(2) position, the second element will be moved to the third position, and so on. As shown down below⇾

Array⇾ [1, 2, 3, 4, 5].
Rotate Left ⇾ [4, 5, 1, 2, 3].

Finally, print the Left rotated array.