Java - Arrays, If, and For ( 8 )

Example 1: Print all elements and size of array

							  
public class ArraysExs {
	public static void main(String[] args) {
		int a[]=new int[5];
		a[0]=10;
		a[1]=20;
		a[2]=30;
		a[3]=40;
		a[4]=50;
		
		System.out.println("Value at 0th index :"+a[0]);
		System.out.println("Value at 1st index :"+a[1]);
		System.out.println("Value at 2nd index :"+a[2]);
		System.out.println("Value at 3rd index :"+a[3]);
		System.out.println("Value at 4th index :"+a[4]);
		
		System.out.println("Array Length :"+a.length);
	}
}

							  

RESULT

Value at 0th index :10
Value at 1st index :20
Value at 2nd index :30
Value at 3rd index :40
Value at 4th index :50
Array Length :5


Example 2: Print all elements using for loop and for each loop

							  
public class ArraysExs {
	public static void main(String[] args) {
		int a[]=new int[5];
		a[0]=10;
		a[1]=20;
		a[2]=30;
		a[3]=40;
		a[4]=50;
 
		System.out.println("************** Printing all elements using for loop *****************");
		for(int i=0;i<a.length;i++)
		{
			System.out.println("Value at "+i+"th index :"+a[i]);
		}
		System.out.println();
		System.out.println("************** Printing all elements using for-each loop *****************");
		for(int temp : a)
		{
			System.out.println("Value : "+temp);
		}

	}
}
							  
							  

RESULT

************** Printing all elements using for loop *****************
Value at 0th index :10
Value at 1th index :20
Value at 2th index :30
Value at 3th index :40
Value at 4th index :50

************** Printing all elements using for-each loop *****************
Value : 10
Value : 20
Value : 30
Value : 40
Value : 50


Example 3: Print all elements, summing, averaging and finding the largest element.

							  
public class ArraysExample1 {
	public static void main(String[] args) {
		int[] marks = {75, 80, 90, 95,87,100};

		// Print all the array elements
		for (int i = 0; i < marks.length; i++) {
			System.out.println(marks[i] + " ");
		}

		// Summing all elements
		int total = 0;
		for (int i = 0; i < marks.length; i++) {
			total += marks[i];
		}
		System.out.println("Total is : " + total);
		float percentage=(float)(total/6.0);
		String final_percentage=String.format("%.2f", percentage) ;
		System.out.println("Percentage is : " + final_percentage+" %");

		// Finding the largest element
		int max = marks[0];
		for (int i = 1; i < marks.length; i++) {
			if (marks[i] > max){
				max = marks[i];
			}
		}
		System.out.println("Max is : " + max);
	}
}

							  

RESULT

75 
80 
90 
95 
87 
100 
Total is : 527
Percentage is : 87.83 %
Max is : 100


Example 4: Find and print even and odd numbers

							  

public class ArraysExample2 {
	public static void main(String[] args) {

		int num[]={11,23,20,22,33,44,77,88,99,90,100,23,11};

		for(int temp_num : num){
			
			if( temp_num % 2 == 0){
				System.out.println(temp_num + " is even number.");
			}
			else
			{
				System.out.println(temp_num + " is odd number.");
			}
			
		}
	}
}

							  

RESULT

11 is odd number.
23 is odd number.
20 is even number.
22 is even number.
33 is odd number.
44 is even number.
77 is odd number.
88 is even number.
99 is odd number.
90 is even number.
100 is even number.
23 is odd number.
11 is odd number.



Example 5: Print all elements before and after adding each by 5.

							  
public class ArraysExs {
	public static void main(String[] args) {
		int a[]=new int[5];
		a[0]=10;
		a[1]=20;
		a[2]=30;
		a[3]=40;
		a[4]=50;
		
		System.out.println("********** Elements before adding each by 5 ***********");
		for(int temp : a)
		{
			System.out.println("Value : "+temp);
		}
		
		System.out.println();
		for(int i=0;i<a.length;i++)
		{
			a[i]=a[i]+5;
		}
		
		System.out.println("********** Elements after adding each by 5 ***********");
		for(int temp : a)
		{
			System.out.println("Value : "+temp);
		}

	}
}
							  
							  

RESULT

********** Elements before adding each by 5 ***********
Value : 10
Value : 20
Value : 30
Value : 40
Value : 50

********** Elements after adding each by 5 ***********
Value : 15
Value : 25
Value : 35
Value : 45
Value : 55


Example 6: Find Largest and Smallest Number in an Array Example

							  
public class ArraysExs {
	public static void main(String[] args) {
		//array of 12 numbers
		int numbers[] = new int[]{32,43,53,54,32,65,01,63,98,43,23,111};

		//assign first element of an array to largest and smallest
		int smallest = numbers[0];
		int largetst = numbers[0];

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

			if(numbers[i] > largetst)
			{
				largetst = numbers[i];
			}

			if (numbers[i] < smallest)
			{
				smallest = numbers[i];
			}

		}

		System.out.println("Largest Number is : " + largetst);
		System.out.println("Smallest Number is : " + smallest);

	}
}

							  

RESULT

Largest Number is : 111
Smallest Number is : 1


Example 7: Printing all array elements in reverse order

							  
public class ArraysExs {
	public static void main(String[] args) {
		
		char[] alphabets = { 'a', 'b', 'c', 'd' };
		System.out.println("********** Array elements  in same order. **********");
		for(char letter : alphabets)
		{
			 System.out.println(letter);
		}
		System.out.println();
		System.out.println("********** Array elements  in reverse order. **********");
		for (int i = alphabets.length - 1; i >= 0; i--) {
		    System.out.println(alphabets[i]);
		}

	}
}
							  
							  

RESULT

********** Array elements  in same order. **********
a
b
c
d

********** Array elements  in reverse order. **********
d
c
b
a


Example 8: Copying elements from one array to another

							  
public class ArraysExs {
	public static void main(String[] args) {
		
		float num1[]={22.55f,33.33f,44.44f,55.55f};
		float num2[]=new float[num1.length];
		
		System.out.println("Default values in num2 array :");
		for(float temp_val:num2){
			System.out.println(temp_val);
		}
		
		System.out.println();
		for(int i=0;i<num1.length;i++)
		{
			num2[i]=num1[i];
		}
		
		System.out.println("Num2 array emlements after copying from num1 array :");
		for(float temp_val:num2){
			System.out.println(temp_val);
		}
	
	}
}
							  
							  

RESULT

Default values in num2 array :
0.0
0.0
0.0
0.0

Num2 array emlements after copying from num1 array :
22.55
33.33
44.44
55.55

BytecodeTechnosolutions.com | Raju