0% found this document useful (0 votes)
28 views

Arrays in Java - Ariharavelava K (ECE - A 012)

Uploaded by

ajaykarthic02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Arrays in Java - Ariharavelava K (ECE - A 012)

Uploaded by

ajaykarthic02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ARRAYS IN JAVA

SUBMITTED BY ARIHARAVELAVA K (ECE-A III YEAR)

1. Maximum and minimum element In an array.

Program:
public class max_min {
public static void main(String[] args) {
int[] a = {10, 20, 30, 40, 50};
int i, min, max;
min = a[0];
max = a[0];
for (i = 0; i < 5; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
System.out.println("maximum number in an array is " + max);
System.out.println("minimum number in an array is " + min);
}}

output:
2. Insert an array element.

Program:
import java.util.Scanner;

public class insert {


public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the array size :");
int n = sc.nextInt();
System.out.print("Enter the array elements :");
int a[] = new int[n];
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.print("Enter the element to be inserted :");
int k = sc.nextInt();
System.out.print("Enter the index position to insert :");
int b = sc.nextInt();
a[b] = k;
for (i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}

Output:
3. Common elements between two arrays.

Program:

public class common_elements {


public static void main(String[] args) {
int[] a = {1,3,4,556,7,89,95,3,556,7,8,99,2};
int[] b = {1,3,5,678,9,44,56,78,556,78,8,95,1};
int i, j;
for (i = 0; i < a.length; i++) {
for(j=0;j <b.length; j++){
if(a[i]==b[j])
System.out.print(a[i]+" ");

}}

}}

output:
4. Display the elements in odd position.

Program:

public class display_odd_position {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40, 50};
int i, sum = 0;
for (i = 0; i < a.length; i++) {
if(i%2==1)
System.out.print(a[i]+" ");
}
}}

output:
5. Print the modified array so that the average of the array is increased by
the size . only one chance is allowed to alter the array value.

Program:

import java.util.Scanner;

public class average_increase_by_5 {


public static void main(String[] args)
{
int i,sum=0;
float avg=0;
Scanner sc = new Scanner(System.in);
System.out.print("enter the array size :");
int n = sc.nextInt();
System.out.print("enter the array elements :");
int a[] = new int[n];
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
a[0]=(n*n)+a[0];
for (i = 0; i < n; i++) {
sum+=a[i];
}
avg=sum/n;
System.out.print(avg);
}
}

output:
6. Display the difference between sum of odd and even position element
in an array.

Program:

import java.util.*;
public class difference_odd_even {
public static void main(String[] args) {
int i, sumodd = 0, sumeven = 0, difference = 0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]= new int[n];
for(i=0;i<n;i++)
{ a[i]=sc.nextInt();}
for (i = 0; i <a.length; i++) {
if (i % 2 == 1)
sumodd += a[i];
else
sumeven += a[i];
}
difference=sumodd-sumeven;
System.out.print("difference between odd and even position of an
array is " +difference);
}
}

output:
7. Find the index of the array element.

Program:

import java.util.Scanner;

public class index {


public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the array size :");
int n = sc.nextInt();
System.out.print("Enter the array elements :");
int a[] = new int[n];
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.print("Enter the number to be searched :");
int k = sc.nextInt();
for (i = 0; i < n; i++) {
if (k == a[i])
break;
}
System.out.print("Index of the element is :" + i);
}
}

output:
8. Add the array elements and display the output.

Program:

public class Add {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40, 50};
int i, sum = 0;
for (i = 0; i < 5; i++) {
sum += a[i];
}
System.out.print("sum of elements in an array is " + sum);
}
}

output:
9. Perform left shifting for ‘K’ times in an array.

Program:

import java.util.*;
public class leftshift_k_times {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("enter the array size :");
int n = sc.nextInt();
System.out.println("enter the number place shift :");
int k = sc.nextInt();
System.out.println("enter the array elements :");
int a[] = new int[n];
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (i = 0; i < n - k; i++) {
a[i] = a[i + k];
}
for (i = n - k; i < n; i++) {
a[i] = 0;
}
for (i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}

output:
10. Perform right shifting for ‘K’ times in an array.

Program:

import java.util.*;
public class rightshift_k_times {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("enter the array size :");
int n = sc.nextInt();
System.out.println("enter the number place shift :");
int k = sc.nextInt();
System.out.println("enter the array elements :");
int a[] = new int[n];
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (i = n; i < k; i++) {
a[i] = a[i-k];
}
for (i = 0; i < k; i++) {
a[i] = 0;
}
for (i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}

output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy