Java Program to Find the Elements from an Array which are Smaller than a Given Number

In this article we are going to see how to Find the Elements from an Array which are Smaller than a Given Number.

Java Program to Find the Elements from an Array which are Smaller than a Given Number

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to Find the Elements from an Array which are Smaller than a Given Number.

Method-1: Java Program to Find the Elements from an Array which are Smaller than a Given Number By Static Initialization of Array Elements

Approach:

  1. Iterate over the array.
  2. Check if any element is smaller than the given number then print.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int num = 5;

        // call the method
        System.out.print("Elements smaller than " + num + " are: ");
        findGreater(arr, num);
    }

    public static void findGreater(int[] arr, int num) 
    {
        for (int i : arr) 
        {
            if (i < num) 
            {
                System.out.print(i + " ");
            }

        }
    }
}

Output:

Elements smaller than 5 are: 1 2 3 4

Method-2: Java Program to Find the Elements from an Array which are Smaller than a Given Number By Dynamic Initialization of Array Elements

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Iterate over the array.
  6. Check if any element is smaller than the given number then print.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        // take input from user for element to be searched
        System.out.print("Enter the number: ");
        int num = sc.nextInt();

        // call the method
        System.out.print("Elements smaller than " + num + " are: ");
        findGreater(arr, num);
    }

    public static void findGreater(int[] arr, int num) 
    {
        // compare each element of array with num
        for (int i : arr) 
        {
            if (i < num) 
            {
                System.out.print(i + " ");
            }

        }
    }
}

Output:

Enter the size of array: 5
Enter array elements: 1 5 3 2 4
Enter the number: 3
Elements smaller than 3 are: 1 2

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Java Program to Find the Elements from the Array which are Greater than the Average of Array

In this article we are going to see how to find the elements from an array which are greater than the average of all elements of the array.

Java Program to Find the Elements from the Array which are Greater than the Average of All Elements of Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the elements from an array which are greater than the average of all elements of the array.

Method-1: Java Program to Find the Elements from the Array which are Greater than the Average of Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Find the average of all elements of the array elements.
  • Iterate over the array.
  • Check if any element is greater than the average of array.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        //initializing sum value as 0
        int sum=0;
        int length=arr.length;
        
        //Finding average of array elements
        for (int i=0; i<arr.length;i++) 
        {
                sum=sum+arr[i];
        }
        
        //find average
        int avg=sum/length;
        System.out.println("Average of array elements : "+avg);

        System.out.print("Elements greater than average " + avg + " are : ");
        // calling the method to find greater elements than the average
        findGreater(arr, avg);
    }
    
    
    //findGreater() method which finds greater elements than the average
    public static void findGreater(int[] arr, int avg) 
    {
         for (int i : arr) 
         {
             if (i > avg) 
             {
                 System.out.print(i + " ");
             }

         }
     }
}
Output:

Average of array elements : 5
Elements greater than average 5 are : 6 7 8 9 10

Method-2: Java Program to Find the Elements from the Array which are Greater than the Average of Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Find the average of all elements of the array elements.
  • Iterate over the array.
  • Check if any element is greater than the average of array.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        //initializing sum value as 0
        int sum=0;
        int length=arr.length;
        
        //Finding average of array elements
        for (int i=0; i<arr.length;i++) 
        {
                sum=sum+arr[i];
        }
        
        //find average
        int avg=sum/length;
        System.out.println("Average of array elements : "+avg);

        System.out.print("Elements greater than average " + avg + " are : ");
        // calling the method to find greater elements than the average
        findGreater(arr, avg);
    }
    
    
    //findGreater() method which finds greater elements than the average
    public static void findGreater(int[] arr, int avg) 
    {
         for (int i : arr) 
         {
             if (i > avg) 
             {
                 System.out.print(i + " ");
             }

         }
     }
}
Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
Average of array elements : 3
Elements greater than average 3 are : 4 5

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Java Program to Find the Elements from the Array which are Smaller than the Average of Array

In this article we are going to see how to find the elements from an array which are smaller than the average of all elements of the array.

Java Program to Find the Elements from the Array which are Smaller than the Average of Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the elements from an array which are smaller than the average of all elements of the array.

Method-1: Java Program to Find the Elements from the Array which are Smaller than the Average of Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Find the average of all elements of the array elements.
  • Iterate over the array.
  • Check if any element is smaller than the average of array.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        //initializing sum value as 0
        int sum=0;
        int length=arr.length;
        
        //Finding average of array elements
        for (int i=0; i<arr.length;i++) 
        {
                sum=sum+arr[i];
        }
        
        //find average
        int avg=sum/length;
        System.out.println("Average of array elements : "+avg);

        System.out.print("Elements smaller than average " + avg + " are : ");
        // calling the method to find smaller elements than the average
        findSmaller(arr, avg);
    }
    
    
    //findSmaller() method which finds smaller elements than the average
    public static void findSmaller(int[] arr, int avg) 
    {
         for (int i : arr) 
         {
             if (i < avg) 
             {
                 System.out.print(i + " ");
             }

         }
     }
}
Output:

Average of array elements : 5
Elements smaller than average 5 are : 6 7 8 9 10

Method-2: Java Program to Find the Elements from the Array which are Smaller than the Average of Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Find the average of all elements of the array elements.
  • Iterate over the array.
  • Check if any element is smaller than the average of array.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        //initializing sum value as 0
        int sum=0;
        int length=arr.length;
        
        //Finding average of array elements
        for (int i=0; i<arr.length;i++) 
        {
                sum=sum+arr[i];
        }
        
        //find average
        int avg=sum/length;
        System.out.println("Average of array elements : "+avg);

        System.out.print("Elements smaller than average " + avg + " are : ");
        // calling the method to find smaller elements than the average
        findSmaller(arr, avg);
    }
    
    
    //findSmaller() method which finds smaller elements than the average
    public static void findSmaller(int[] arr, int avg) 
    {
         for (int i : arr) 
         {
             if (i < avg) 
             {
                 System.out.print(i + " ");
             }

         }
     }
}
Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
Average of array elements : 3
Elements smaller than average 3 are : 4 5

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Java Program to Count Positive Negative and Zero from Array

In this article we are going to see how we can count the number of negative, positive and zero elements by using Java programming language.

Java Program to Count Positive Negative and Zero from Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to count positive, negative and zero element in an array.

Method-1: Java Program to Count Positive Negative and Zero from Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate the array.
  • If array element is greater than zero then it is positive number so increase the positive count.
  • If array element is smaller than zero then it is negative number so increase the negative count.
  • Else element is equal to zero then it is zero so increase the zero count.
  • Then print the result.

Program:

public class Main
{
    public static void main(String args[])
    {
        // Array with elements
        int arr[] = {10,-15,1,-30,50,7,1,0,0};
        
        //variable to store count of positive, negative and zero elements
        //initialized with to 0
        int positive=0,negative=0,zero=0;
        
        //Loop to count positive,negative and zero elements
        for(int row=0;row<arr.length;row++)
        {
                //if array element is greater than zero it is positive
                if(arr[row]>0)
                    //incrementing positive count value
                    positive++;
                    
                //if array element is smaller than zero it is negative
                else if(arr[row]<0)
                    //incrementing negative count value
                    negative++;
                //if array element is not greater or smaller than zero then it is equal to zero
                else
                    //incrementing zero count value
                    zero++;
        } 
        
        System.out.println("Number of positive elements are : "+positive);
        System.out.println("Number of negative elements are : "+negative);
        System.out.println("Number of zero elements are : "+zero);
    }
}
Output:

Number of positive elements are : 5
Number of negative elements are : 2
Number of zero elements are : 2

Method-2: Java Program to Count Positive Negative and Zero from Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size input from user.
  • Take the input of array elements.
  • Print the array.
  • Then Iterate the array.
  • If array element is greater than zero then it is positive number so increase the positive count.
  • If array element is smaller than zero then it is negative number so increase the negative count.
  • Else element is equal to zero then it is zero so increase the zero count.
  • Then print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Array with elements
        int arr[] = null;
        
        System.out.print("Enter the length of the array : ");
        int length = scan.nextInt();
        arr = new int[length];
        int iter;  
        
        // Entering the array elements
        System.out.println("Enter the array elements : ");
        for(iter=0;iter<arr.length;iter++)
            arr[iter]=scan.nextInt();
            
        System.out.println("The array elements are : ");
        //For Loop to print the elements
        for(iter=0;iter<arr.length;iter++)
        {
                System.out.print(arr[iter]+" ");
        }   
        
        //variable to store count of positive,negative and zero elements
        //initialized with to 0
        int positive=0,negative=0,zero=0;
        
        //Loop to count positive,negative and zero elements
        for(int row=0;row<arr.length;row++)
        {
                //if array element is greater than zero it is positive
                if(arr[row]>0)
                    //incrementing positive count value
                    positive++;
                    
                //if array element is smaller than zero it is negative
                else if(arr[row]<0)
                    //incrementing negative count value
                    negative++;
                //if array element is not greater or smaller than zero then it is equal to zero
                else
                    //incrementing zero count value
                    zero++;
        } 
        
        System.out.println("\nNumber of positive elements are : "+positive);
        System.out.println("Number of negative elements are : "+negative);
        System.out.println("Number of zero elements are : "+zero);
    }
}
Output:

Enter the length of the array : 10
Enter the array elements : 
1  -2  3  -4  5  0  6  0  -7  8 
The array elements are : 
1  -2  3  -4  5  0  6  0  -7  8 
Number of positive elements are : 5
Number of negative elements are : 3
Number of zero elements are : 2

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Java Program to Separate Positive Negative and Zero elements from Array and Store in Separate arrays

In this article we are going to see how we can separate negative, positive and zero elements in separate arrays by using Java programming language.

Java Program to Separate Positive Negative and Zero elements from Array and Store in Separate arrays

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to separate negative, positive and zero elements in separate arrays.

Method-1: Java Program to Separate Negative, Positive and Zero Elements in Separate Arrays By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate the array.
  • If array element is greater than zero then it is positive number so store in the positive array.
  • If array element is smaller than zero then it is negative number so store in the negative array.
  • Else element is equal to zero then it is zero so store in the zero array.
  • Then print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        // Array with elements 
        int arr[] = {10,-15,1,-30,50,7,1,0,0};
            
        System.out.println("The array elements are : ");
        //For Loop to print the elements
        for(int iter=0;iter<arr.length;iter++)
        {
                System.out.print(arr[iter]+" ");
        }
        
        //variable to store positive,negative and zero elements in separate arrays
        //declaring the arrays with size of actual array
        int positive[] = new int[arr.length];
        int negative[] = new int[arr.length];
        int zero[] = new int[arr.length];
        
        int i=0;
        int j=0;
        int k=0;
        //Loop to count positive,negative and zero elements
        for(int row=0;row<arr.length;row++)
        {
                //if array element is greater than zero it is positive
                if(arr[row]>0)
                {
                    //storing the positive value in positive array
                    positive[i]=arr[row];
                    i++;
                }
                    
                //if array element is smaller than zero it is negative
                if(arr[row]<0)
                {
                    //storing the negative value in negative array
                    negative[j]=arr[row];
                    j++;
                }
                    
                //if array element is not greater or smaller than zero then it is equal to zero
                if(arr[row]==0)
                {
                    //storing the zero value in zero array
                    zero[k]=arr[row];
                    k++;
                }
                
        } 
        
        System.out.println("\nPositive array : ");
        printArray(positive,i);
        System.out.println("\nNegative array : ");
        printArray(negative,j);
        System.out.println("\nZero Array : ");
        printArray(zero,k);
    }   
        
        //printArray() method to print array
        public static void printArray(int[] arr, int counter)
        {
           for(int a=0;a<counter;a++)
           {
               System.out.print(arr[a]+" ");
           }
        }
}
Output:
The array elements are : 
10 -15 1 -30 50 7 1 0 0 
Positive array : 
10 1 50 7 1 
Negative array : 
-15 -30 
Zero Array : 
0 0

Method-2: Java Program to Separate Negative, Positive and Zero Elements in Separate Arrays By Dynamic Initialization of Array Elements

Approach:

  • Take the array size input from user.
  • Take the input of array elements.
  • Print the array.
  • Then Iterate the array.
  • If array element is greater than zero then it is positive number so store in the positive array.
  • If array element is smaller than zero then it is negative number so store in the negative array.
  • Else element is equal to zero then it is zero so store in the zero array.
  • Then print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Array with elements
        int arr[] = null;
        
        System.out.print("Enter the length of the array : ");
        int length = scan.nextInt();
        arr = new int[length];
        int iter;  
        
        // Entering the array elements
        System.out.println("Enter the array elements : ");
        for(iter=0;iter<arr.length;iter++)
            arr[iter]=scan.nextInt();
            
        System.out.println("The array elements are : ");
        //For Loop to print the elements
        for(iter=0;iter<arr.length;iter++)
        {
                System.out.print(arr[iter]+" ");
        }
        
        //variable to store positive,negative and zero elements in separate arrays
        //declaring the arrays with size of actual array
        int positive[] = new int[arr.length];
        int negative[] = new int[arr.length];
        int zero[] = new int[arr.length];
        
        int i=0;
        int j=0;
        int k=0;
        //Loop to count positive,negative and zero elements
        for(int row=0;row<arr.length;row++)
        {
                //if array element is greater than zero it is positive
                if(arr[row]>0)
                {
                    //storing the positive value in positive array
                    positive[i]=arr[row];
                    i++;
                }
                    
                //if array element is smaller than zero it is negative
                if(arr[row]<0)
                {
                    //storing the negative value in negative array
                    negative[j]=arr[row];
                    j++;
                }
                    
                //if array element is not greater or smaller than zero then it is equal to zero
                if(arr[row]==0)
                {
                    //storing the zero value in zero array
                    zero[k]=arr[row];
                    k++;
                }
                
        } 
        
        System.out.println("\nPositive array : ");
        printArray(positive,i);
        System.out.println("\nNegative array : ");
        printArray(negative,j);
        System.out.println("\nZero Array : ");
        printArray(zero,k);
    }   
        
        //printArray() method to print array
        public static void printArray(int[] arr, int counter)
        {
           for(int a=0;a<counter;a++)
           {
               System.out.print(arr[a]+" ");
           }
        }
}
Output:

Enter the length of the array : 6
Enter the array elements : 
1 2 -3 -4 0 0
The array elements are : 
1 2 -3 -4 0 0 
Positive array : 
1 2 
Negative array : 
-3 -4 
Zero Array : 
0 0

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Java Program to Sort String Elements in an Alphabetical Order

In this article we are going to see how to sort string elements in alphabetical order using Java programming language.

Java Program to Sort String Elements in an Alphabetical Order

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to sort string elements in an alphabetical order in an array.

Method-1: Java Program to Sort String Elements in an Alphabetical Order By Using compareTo() Method

Approach:

  1. Run two nested loops one i = 0 to arr.length, second one j = i+1 to arr.length.
  2. Using CompareTo()method compare one string with the rest of the strings.
  3. If it returns > 0 then the parameter passed to compareTo() method is lexicographically first, so swap the elements.
  4. Else the elements are lexicographically correct.
  5. Print the Sorted elements in an Alphabetical Order.

Program:

public class Main
{

    public static void main(String[] args) 
    {
        // initialize the array
        String[] words = { "apple", "dog", "boy", "cat" };
        // call the method
        sortAlphabetically(words);
    }

    static void sortAlphabetically(String[] words) 
    {
        String temp;
        for (int i = 0; i < words.length; i++) 
        {
            for (int j = i + 1; j < words.length; j++) 
            {

                // to compare one string with other strings
                if (words[i].compareTo(words[j]) > 0) 
                {
                    // swapping
                    temp = words[i];
                    words[i] = words[j];
                    words[j] = temp;
                }
            }
        }

        // print output array
        System.out.print("The words in alphabetical order are: ");
        for (int i = 0; i < words.length; i++) {
            System.out.print(words[i] + " ");
        }
    }
}
Output:

The words in alphabetical order are: apple boy cat dog

Method-2: Java Program to Sort String Elements in an Alphabetical Order By Using Arrays.sort() Method

Approach:

  • Create scanner class object.
  • Ask the user for number of arrays.
  • Ask the user to enter the size of each individual list and the its elements one by one.
  • Sort the array using Arrays.sort() method.
  • Print the sorted array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main 
{

    public static void main(String[] args) 
    {
       // create scanner class object
       Scanner sc = new Scanner(System.in);
       // take input from user for array size
       System.out.print("Enter the number of words: ");
       int n = sc.nextInt();
       // extra sc.nextLine(); to consume the new line character
       sc.nextLine();
       // initialize array with size n
       String[] words = new String[n];
       // take input from user for array elements
       System.out.println("Enter the words: ");
       for (int i = 0; i < n; i++) {
           words[i] = sc.nextLine();
       }
        sortAlphabetically(words);
    }

    static void sortAlphabetically(String[] words) 
    {
        // sorting the array
        Arrays.sort(words);
        System.out.print("The words in alphabetical order are: " + Arrays.toString(words));

    }
}

Output:

Enter the number of words: 6
Enter the words: 
dog
ant
cat
bat
fan
ear
The words in alphabetical order are: [ant, bat, cat, dog, ear, fan]

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Java Program to Increment Each Element of Array by 1 and Print the Incremented Array

In this article we are going to see how increment array element by one using Java programming language.

Java Program to Increment Each Element of Array by 1 and Print the Incremented Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to increment each element of array by 1 and print the incremented array.

Method-1: Java Program to Increment Each Element of Array by 1 and Print the Incremented Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate over the array.
  • Increment each element by one.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        int arr[]={10,20,30,40,50};
        System.out.println("Original array: " + Arrays.toString(arr));
        IncrementByOne(arr);
    }

    private static void IncrementByOne(int[] arr) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]++;
        }
        System.out.println("Incremented array: " + Arrays.toString(arr));
    }

}
Output:

Original array: [10, 20, 30, 40, 50]
Incremented array: [11, 21, 31, 41, 51]

Method-2: Java Program to Increment Each Element of Array by 1 and Print the Incremented Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size from user.
  • Take input of array elements as user input.
  • Iterate over the array.
  • Increment each element by one.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.println("Original array: " + Arrays.toString(arr));
        IncrementByOne(arr);
    }

    private static void IncrementByOne(int[] arr) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]++;
        }
        System.out.println("Incremented array: " + Arrays.toString(arr));
    }

}
Output:

Enter the size of array: 5
Enter array elements: 10 20 30 40 50
Original array: [10, 20, 30, 40, 50]
Incremented array: [11, 21, 31, 41, 51]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array

In this article we are going to see how decrement array element by one using Java programming language.

Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to decrement each element of array by 1 and print the incremented array.

Method-1: Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate over the array.
  • Decrement each element by one.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        int arr[]={10,20,30,40,50};
        System.out.println("Original array: " + Arrays.toString(arr));
        DecrementByOne(arr);
    }

    private static void DecrementByOne(int[] arr) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]--;
        }
        System.out.println("Decremented array: " + Arrays.toString(arr));
    }

}
Output:

Original array: [10, 20, 30, 40, 50]
Decremented array: [9,19,29,39,49]

Method-2: Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size from user.
  • Take input of array elements as user input.
  • Iterate over the array.
  • Decrement each element by one.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.println("Original array: " + Arrays.toString(arr));
        DecrementByOne(arr);
    }

    private static void DecrementByOne(int[] arr) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]--;
        }
        System.out.println("Decremented array: " + Arrays.toString(arr));
    }

}
Output:

Enter the size of array: 5
Enter array elements: 10 20 30 40 50
Original array: [10, 20, 30, 40, 50]
Decremented array: [9,19,29,39,49]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Java Program to Decrement Each Element of Array by a Specified Number

In this article we are going to see how decrement array element by a specified number using Java programming language.

Java Program to Decrement Each Element of Array by a Specified Number and Print the Decremented Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to decrement each element of array by a specified number and print the incremented array.

Method-1: Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate over the array.
  • Decrement each element by a specified number.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        int arr[]={10,20,30,40,50};
        int n=5;
        System.out.println("Original array: " + Arrays.toString(arr));
        Decrement(arr,n);
    }

    private static void Decrement(int[] arr, int n) 
    {
        for (int i=0; i<arr.length; i++) 
        {
            arr[i]=arr[i]-n;
        }
        System.out.println("Decremented array: " + Arrays.toString(arr));
    }

}
Output:

Original array: [10, 20, 30, 40, 50]
Decremented array: [5, 15, 25, 35, 45]

Method-2: Java Program to Decrement Each Element of Array by a Specified Number and Print the Decremented Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size from user.
  • Take input of array elements as user input.
  • Iterate over the array.
  • Decrement each element by a specified number.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        // take input from user for array size 
        System.out.print("Enter the specified number : "); 
        int num = sc.nextInt();
        System.out.println("Original array: " + Arrays.toString(arr));
        Decrement(arr,num);
    }

    private static void Decrement(int[] arr,int num) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]=arr[i]-num;
        }
        System.out.println("Decremented array: " + Arrays.toString(arr));
    }

}
Output:

Enter the size of array: 5
Enter array elements: 10 20 30 40 50
Enter the specified number : 5
Original array: [10, 20, 30, 40, 50]
Decremented array: [5, 15, 25, 35, 45]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Java Program to Increment Each Element of Array by a Specified Number

In this article we are going to see how increment array element by a specified number using Java programming language.

Java Program to Increment Each Element of Array by a Specified Number

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to increment each element of array by a  specified number and print the incremented array.

Method-1: Java Program to Increment Each Element of Array by a Specified Number and Print the Incremented Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate over the array.
  • Increment each element by a specified number.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        int arr[]={10,20,30,40,50};
        int n=5;
        System.out.println("Original array: " + Arrays.toString(arr));
        Increment(arr,n);
    }

    private static void Increment(int[] arr, int n) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]=arr[i]+n;
        }
        System.out.println("Incremented array: " + Arrays.toString(arr));
    }

}
Output:

Original array: [10, 20, 30, 40, 50]
Incremented array: [15, 25, 35, 45, 55]

Method-2: Java Program to Increment Each Element of Array by a Specified Number and Print the Incremented Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size from user.
  • Take input of array elements as user input.
  • Iterate over the array.
  • Increment each element by a specified number.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.print("Enter the specified number: ");
        int num=sc.nextInt();
        System.out.println("Original array: " + Arrays.toString(arr));
        Increment(arr,num);
    }

    private static void Increment(int[] arr,int num) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]=arr[i]+num;
        }
        System.out.println("Incremented array: " + Arrays.toString(arr));
    }

}
Output:

Enter the size of array: 5
Enter array elements: 10 20 30 40 50
Original array: [10, 20, 30, 40, 50]
Incremented array: [15, 25, 35, 45, 55]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.