ARRAY PROGRAMS


Today we gonna see some array programs.
In below program we gonna see how many times a number present in the array.
package Array;

public class FindnoOfTimeaNumberPresent {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] a= {10,20,30,40,10};
    int no=10;
    int count=0;
    for(int i=0;i<a.length;i++) {
        if(no==a[i]) {
            count++;
        }

    }
    System.out.println(count);  
}

}

Result:
2

In this program we using linear search to find given number.
package Array;

public class LinearSearch {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int a[]= {1,2,3,4,5};
for(int i=0;i<a.length;i++) {
if(4==a[i]) {
System.out.println(” I GOT IT”);

}

}
}

}

Result:
I GOT IT

In this program we remove a specific number in array list.
package Array;

public class RemovingGivenNumberinArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {10,20,30,40,50};
int no=30;
for(int i=0;i<a.length;i++) {
if(no!=a[i]) {
System.out.println(a[i]);
}
}

}

}

Result:
10
20
40
50

In this program we assign array length, while program running,with using Scanner class.

package selva.w4;

import java.util.Scanner;

public class Arrayusingforloop {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter length");
    int len=sc.nextInt();
    int[] marks1= new int[len];

for(int i=0; i<len; i++)
{
System.out.println(“enter marks”);

marks1[i]=sc.nextInt();

}
}

}

Result:
enter length

3
enter marks
10
enter marks
15
enter marks
20

In this program we count how many times both numbers present in this array.
package selva.w4;

public class CountBothNoinArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] array= {10,20,10,30,40,10,25};
    int search1=10,count1=0,i=0,search2=20,count2=0;
    while(i<array.length)
    {
        if(array[i]==search1) {
    count1++;
        }
        else if(array[i]==search2)
        {
            count2++;
        }
        i++;

        }
        System.out.println("ANS:no of time present is "  + count1);
        System.out.println("ANS:no of time present is "  + count2);
    }
}

Result:
ANS:no of time present is 3
ANS:no of time present is 1

In this program we gonna find how many times number 5 present in array.
package selva.w4;

public class CountfivepresentinNumbers {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {12,45,30,85,95,105};
int i=0;
int count=0;
int no2;
int no1;
while(i<a.length) {
no1=a[i]%10;no2=a[i]/10;
if(no1==5||no2==5) {
count++;
}
i++;
}

System.out.println(count);
}

}

Result:
4

In this program find number index of an number.
package selva.w4;

public class Findarray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] array= {5,10,15,6,3};

        for(int i=0; i<array.length-1;i++)
            if(array[i]==15)
    {
        System.out.println("15 is present at "+i);
        break;
    }

}

}

Result:
15 is present at 2

In this program we gonna find first and second largest number in array.

package selva.w4;

public class FindFirstandSecondLargestValueInArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] no= {3,7,2,8,5};
    int max=Integer.MIN_VALUE;
            int max2=Integer.MIN_VALUE;
            int i=0;
    while(i<no.length) {
        if(no[i]>max) {
            max2=max;
            max=no[i];

        }
        else if(no[i]>max2) {
            max2=no[i];

    }
    i++;
}
    System.out.println("first largest "+max);

System.out.println(“second larges “+max2);
}
}

Result:
first largest 8
second larges 7

In this program we gonna find smallest number present in array.
package selva.w4;

public class FindMinValueinArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] no= {3,7,2,8,5};
    int min=Integer.MAX_VALUE,i=0;
    while(i<no.length) {
        if(min>no[i]) {
            min=no[i];

        }
        i++;
    }
    System.out.println("the smallest number is "+min);
}

}

Result:
the smallest number is 2

In this program we merge two array.
package selva.w4;

public class MergeTwoArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {10,20,30};
int[] b= {5,15};
int[] result=new int[a.length+b.length];
int i=0;
while(i<a.length)
{
result[i]=a[i];
System.out.println(result[i]);
i++;

}
int j=i,k=0;
while(j<result.length)
{
result[j]=b[k];
System.out.println(result[j]);
k++;
j++;
}
}

}

Result:
10
20
30
5
15

In this program we shift towards array towards left.

package selva.w4;

public class ShiftTowardsLeftArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {15,20,25,20,25};
int i=0;
int temp1=a[0];
int temp2=a[1];
while(i<3)
{

a[i]=a[i+2];
System.out.println(a[i]);
i++;

}
System.out.println(temp1);
System.out.println(temp2);
}
}

Result:
25
20
25
15
20

In this program we store and even index number.
package selva.w4;

public class StoreEvenIndexElementsInArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] arr= {10,20,30,40,50};
int[] result= new int[arr.length/2];
int i=0,j=0;
while(i<result.length) {
result[i]=arr[j];
System.out.println(result[i]);
i++;
j+=2;
}
}

}

Result:
10
30

package selva.w4;

public class SwapingArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] arr1= {3,8,2,5,4};
int len=arr1.length;
int[] arr2=new int[len];//take the length of largest array
int i=0,j=arr1.length-1;
while(i<arr1.length)
{
arr2[i]=arr1[j];
System.out.println(arr2[i]);
i++;
j–;
}

}

}

Result:
4
5
2
8
3

Leave a comment

Design a site like this with WordPress.com
Get started