Nov 24, 2021

How to Print Arrays in Java | Java Program to Accept and Print Array Elements

How to print arrays in java: 

To print array elements we first need to accept it from user. In this post we will learn how to accept array elements from user dynamically and print it on the output screen. Simple java program to insert elements in array. 

We can print array elements using any type of loop like :

  • for loop (we can also use for-each loop)
  • while loop
  • do-while loop


  Step by Step logic of the Given Program :


We can take array input from user using the method of the Scanner class. 

1. Accept array elements from user one by one using simple for loop:

for(int i=0;i<5;i++) {
	a[i]=sc.nextInt();		
}
2. Print Elements using for loop in this program i used Enhanced for loop to print array elements:
for(int e:a) {	
	System.out.print(e+" ");
}

but, you can also use simple for loop to print array elements like:

for(int i=0;i<5;i++) {
	System.out.print(a[i]+" ");
}



  Simple Array Program to Accept and Print Array Elements:


import java.util.Scanner;

public class SimpleArrayExample {

	public static void main(String[] args) {
		//Declaration and instantiation
		int  a[] = new int[5];
		Scanner sc = new Scanner(System.in);
		//Accepting elements using simple for loop
		System.out.println("ENTER THE ELEMENTS: ");
		for(int i=0;i<5;i++) {
			a[i]=sc.nextInt();		
		}
		System.out.println("ELEMENTS IN ARRAY ARE: ");
		//printing elements using Enhanced for loop(for-each loop)
		for(int e:a) {
			System.out.print(e+" ");
		}
	}		
}



  Same above Program using while loop:

In above program we have used for loop to store and print array elements. So, In this program we will use while loop to store and print array elements.


import java.util.Scanner;

public class SimpleArrayExample {

	public static void main(String[] args) {
		//Declaration and instantiation
		int  a[] = new int[5];
		Scanner sc = new Scanner(System.in);
		//Accepting elements using while loop
		System.out.println("ENTER THE ELEMENTS: ");
		int i=0;
		while(i<5) {
			a[i]=sc.nextInt();
			i++;
		}
		System.out.println("ELEMENTS IN ARRAY ARE: ");
		//printing elements using while loop
		int j=0;
		while(j<5) {
			System.out.print(a[j]+" ");
			j++;
		}
	}		
}


  

 
 Output of the above program is :

ENTER THE ELEMENTS:
3
8
2
1
ELEMENTS IN ARRAY ARE: 
5 3 8 2 1



Learn more basic concepts of arrays in C array tutorial...

continue reading How to Print Arrays in Java | Java Program to Accept and Print Array Elements
Share This:    Facebook Twitter

How to Sort Arrays in Java

In this post we will learn how to write a  java program to sort array elements. How to sort arrays in java. Sorting array elements means storing or printing it in ascending or descending order. So let's see how to print given array elements in sorted manner.

Basically there are two ways to sort array elements:

  1. Using sort() method
  2. Using for loop


1. Using sort() method:

  Step by step logic to print array elements in ascending order using sort() method:


1. To use sort() method first we need to import Arrays class which is defined in java.util package.

2. Define a array of integer(int) type.

3. Use sort() method to sort array elements like: Arrays.sort(arr);

4. Now print array elements using for loop or any other loop.

5. It will print all array elements sorted in ascending order.  



  Program to Print Array Elements Sorted in Ascending Order using sort() method:


/**Program to sort array elements in Ascending order*/

import java.util.Arrays;//importing the Arrays class

public class ArraySortingExample {

	public static void main(String[] args) {
		
		int arr[] = new int[] {55,7,12,3,9,23,67,2};
		//Sorting elements using predefined method
		Arrays.sort(arr);
		//printing elements
		System.out.println("Elements after sorting: ");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}
	}

}


  Output:

Elements after sorting:
2
7
9
12
23
55
67


2. Same above Program Without using predefined method Using for loop:

This program is same as above program but, only change is... in this program we have used following logic instead of sort() method:
for(int i=0;i<arr.length;i++) {
	for(int j=i+1;j<arr.length;j++) {
		if(arr[i]>arr[j]) {
		temp = arr[i];
		arr[i]=arr[j];
		arr[j]=temp;
		} 
	}
}
  


  Program:


/**Program to sort array elements in Ascending order*/
public class ArraySortingExample2 {

	public static void main(String[] args) {
		int arr[] = new int[] {55,7,12,3,9,23,67,2};
		int temp;
		//sorting elements logic
		for(int i=0;i<arr.length;i++) {
			for(int j=i+1;j<arr.length;j++) {
				if(arr[i]>arr[j]) {
					temp = arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				} 
			}
		}
		
		//printing elements
		System.out.println("Elements after sorting: ");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}

	}
}

  Output:

Elements after sorting:
2
7
9
12
23
55
67



  Program to Sort array elements in Descending Order:

Sorting arrays in descending order means storing or printing the elements in highest to lowest order.



/**Program to sort array elements in descending order*/
public class ArraySortingExample3 {

	public static void main(String[] args) {
		int arr[] = new int[] {55,7,12,3,9,23,67,2};
		int temp;
		//sorting elements logic
		for(int i=0;i<arr.length;i++) {
			for(int j=i+1;j<arr.length;j++) {
				if(arr[i]<arr[j]) {
					temp = arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				} 
			}
		}
		
		//printing elements
		System.out.println("Elements after sorting: ");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}

	}

}

  Output of the above program is :

Elements after sorting: 
67
55
23
12
9
7
3
2


continue reading How to Sort Arrays in Java
Share This:    Facebook Twitter

Nov 22, 2021

Java Program to Print Square of Given Array Elements

 In previous post we learnt how to print multiplication of given array elements. In this post we will learn how to print squares of given array elements. So let's get started... :)


  Step by step logic of the given program:


1. Accept array limit from user store it in variable say lim.

2. Accept elements from user. Run a for loop from to arr.length-1 and store elements one by one in array:

for(int i =0;i<arr.length;i++) {
	arr[i]=sc.nextInt();
}

3. After that store Square of array elements one by one in variable called square and print it.

for(int i=0;i<arr.length;i++) {
	square = arr[i]*arr[i];
	System.out.println("Square of "+ arr[i] +" is: "+square);
    }


  Java Program to Print Square of Given Array Elements :




import java.util.Scanner;

public class ArraySquareExample {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int square=0;
		//Accept limit
		System.out.println("How many elements you want to enter: ");
		int lim = sc.nextInt();
		int[] arr= new int[lim];
		//Accepting elements
		System.out.println("Enter "+lim+" elements: ");
		for(int i =0;i<arr.length;i++) {
			arr[i]=sc.nextInt();
		}
		//Logic to print square of array elements
		for(int i=0;i<arr.length;i++) {
			square = arr[i]*arr[i];
			System.out.println("Square of "+ arr[i] +" is: "+square);
	    }

	}
}

Output of the above program is:

How many elements you want to enter:
7
Enter 7 elements:
5
3
8
7
9
15
21
Square of 5 is: 25
Square of 3 is: 9
Square of 8 is: 64
Square of 7 is: 49
Square of 9 is: 81
Square of 15 is: 225
Square of 21 is: 441


continue reading Java Program to Print Square of Given Array Elements
Share This:    Facebook Twitter

Java Program to Print Multiplication of Given Array Elements

In previous post we learnt how to print Sum of given array elements. In this post we will learn how to print multiplication of given array elements. 


  Step by step logic of the given program:


1. Accept array limit from user store it in variable say lim.

2. Accept elements from user. Run a for loop from to arr.length-1 and store elements one by one in array:

for(int i =0;i<arr.length;i++) {
	arr[i]=sc.nextInt();
}

3. After that store multiplication of  all elements in variable called mult using for-each loop (you can also use simple for loop).

for(int ele:arr) {
	mult*=ele;
}

4. Last print multiplication of array elements stored in mult variable.



  Java Program to Print Multiplication of Given Array Elements :




import java.util.Scanner;

public class ArrayMultExample {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int mult=1;
		//Accept limit
		System.out.println("How many elements you want to enter: ");
		int lim = sc.nextInt();
		int[] arr= new int[lim];
		//Accepting elements
		System.out.println("Enter "+lim+" elements: ");
		for(int i =0;i<arr.length;i++) {
			arr[i]=sc.nextInt();
		}
		//Storing multiplication in mult variable
		for(int ele:arr) {
			mult*=ele;
		}
		System.out.println("Multiplication of given array elements is: "+mult);

	}

}

Output of the above Program is:

How many elements you want to enter : 
5
Enter 5 elements:
1
2
3
4
5
Multiplication of given array elements is : 120


continue reading Java Program to Print Multiplication of Given Array Elements
Share This:    Facebook Twitter

Nov 21, 2021

Java Program to Print Sum of given Array Elements

In previous  post we learnt how to check given element is present in array or not. In this post we will learn how to print sum of given array elements. So let's get started... :)


  Step by step logic of the given program:


1. Accept array limit from user store it in variable say lim.

2. Accept elements from user. Run a for loop from 0 to arr.length-1 and store elements one by one in array:

for(int i =0;i<arr.length;i++) {
	arr[i]=sc.nextInt();
}

3. After that store sum of  all elements in variable called sum using for-each loop (you can also use simple for loop).

for(int ele:arr) {
	sum+=ele;
}

4. Last print sum of array elements stored in sum variable.



  Java Program to Print Sum of Given Array Elements :



import java.util.Scanner;

public class ArraySumExample {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int sum=0;
		
		System.out.println("How many elements you want to enter: ");
		int lim = sc.nextInt();
		int[] arr= new int[lim];
		//Accept Elements
		System.out.println("Enter "+lim+" elements: ");
		for(int i =0;i<arr.length;i++) {
			arr[i]=sc.nextInt();
		}
		//for-each loop
		for(int ele:arr) {
			sum+=ele;
		}
		System.out.println("Sum of given array elements is: "+sum);

	}

}


Output of the above program is

How many elements you want to enter:
5
Enter 5 elements: 
5
7
8
10
20
Sum of given array elements is: 50




continue reading Java Program to Print Sum of given Array Elements
Share This:    Facebook Twitter

Java Program to Check whether Given Element is Present in Array or not

In previous tutorial we learnt how to accept and print array elements. In this post we will learn is given element is present in array or not, Java program to check whether given element is present in array or not.


  Step by step logic of the given program:


1. Declare and Initialize array and other variables.

2. Input value from user and store it in variable say value.

System.out.println("Enter the value you want to find: ");
int value = obj.nextInt();

3. Run a loop from 0 to arr.length-1(In below program it will run from 0 to 7) because in below program array length is 8.

4. Inside the for loop use one if condition to check whether given value and array value is same or not, if value is same then increment count by one and break the loop. 

for(i=0; i<arr.length;i++) {
	if(arr[i]==value) {
		count++;
		break;
	}
}

5. Outside the loop use one if condition to check whether the count is greater than zero or not, if true then print value is present in array otherwise print value is not present.



  Java Program to Check whether Given Element is Present in Array or not:





import java.util.Scanner;

public class SearchArrayElement {

	public static void main(String[] args) {
		//Array Initialization
		int arr[] = {5,8,2,4,1,9,3,7};
		int count=0,i;
		
		Scanner obj = new Scanner(System.in);
		//Accept value from user to check whether it is present or not
		System.out.println("Enter the value you want to find: ");
		int value = obj.nextInt();
		//Run a for loop
		for(i=0; i<arr.length;i++) {
			if(arr[i]==value) {
				count++;
				break;
			}
		}
		//If count is greater than zero means value is present
		if(count>0) {
			System.out.println("Value is present in array at position : "+(++i));
		}else {
			System.out.println("Given value is not present in array");
		}
	}

}

In above program we have used length attribute to find length of array.


  Same Above Program using for-each loop:


import java.util.Scanner;

public class SearchArrayElement {

	public static void main(String[] args) {
		//Array Initialization
		int arr[] = {5,8,2,4,1,9,3,7};
		int count=0,i=0;
		
		Scanner obj = new Scanner(System.in);
		//Accept value from user to check whether it is present or not
		System.out.println("Enter the value you want to find: ");
		int value = obj.nextInt();
		//Run for-each loop
		for(int ele:arr) {
			i++;
			if(ele==value) {
				count++;
				break;
			}
		}
		//If count is greater than zero means value is present
		if(count>0) {
			System.out.println("Value is present in array at position : "+i);
		}else {
			System.out.println("Given value is not present in array");
		}
	}

}


Output:

Enter the value you want to find : 
1
Value is present in array at position : 5


continue reading Java Program to Check whether Given Element is Present in Array or not
Share This:    Facebook Twitter

Oct 28, 2021

Factorial Program In Java

Factorial Program in Java:

Factorial of a number means the product of all positive descending integers. 
for example: If we want to calculate factorial of 5 then it will be written as 
5 x 4 x 3 x 2 x 1 = 120 . So 120 is the factorial of  5.
  

  Program to Print Factorial of a given number:



import java.util.Scanner;

public class Factorial {

	public static void main(String[] args) {
		
		int num, fact=1;
		
		Scanner sc = new Scanner(System.in);
		
		//Accepting input
		System.out.println("Enter a Number: ");
		num = sc.nextInt();
		
		for(int i=num; i>=1; i--) {
			fact = fact*i;
		}
		System.out.println("Factorial Of "+num+" is: "+fact);
	}

}



  Factorial Program using while loop:



import java.util.Scanner;

public class Factorial {

	public static void main(String[] args) {
		
		int num, fact=1;
		
		Scanner sc = new Scanner(System.in);
		
		//Accepting input
		System.out.println("Enter a Number: ");
		num = sc.nextInt();
		
		int i = num;
		
		while(i>=1) {
			fact = fact*i;
			i--;
		}
		System.out.println("Factorial Of "+num+" is: "+fact);
	}

}

  

Output:

Enter a Number: 7
Factorial of 7 is: 5040


continue reading Factorial Program In Java
Share This:    Facebook Twitter