Array Programs
Array Programs
1.Write a program to print the 2nd and 4th elements in the given array?
output should be 3.
int[] intArray = new int[]{1,3,2,6,5,4};
2.Write a program to print the 2nd repeated element in the given array.
output should be 3.
int[] intArray = new int[]{1,1,3,1,2,6,3,5,4};
a)int[2][]x={(1,2),(3,4,5)};
b)int[][]x={(1,2),(3,4,5)};
c)int[][]x={1,2.3,4,5};
1
ARRAY PROGRAMS
d)All
a)i i i i i
b)0 1 2 3 4
c)1 2 3 4 5
d)CE:Variable must provide either dimension expressings of an array initialization.
2
ARRAY PROGRAMS
7.Write a program to find common elements between two arrays.
input:['g','y','p','q','r'],['q','t','i','p']
output:q,p
12.Write a program to print swap two numbers without using third variable.
15.Write a program to print even index and odd index elements in an array.
3
ARRAY PROGRAMS
intarr[] = new int[]{0,1,2,3,4,5,6,7,8,9}
int n=6;
n=arr[arr[n]/2];
System.out.println(arr[n]/2);
}
}
a)3
b)0
c)6
d)1
4
ARRAY PROGRAMS
18.W.A.P to find duplicate elements in the given array and print their sum.
Array = {5,3,4,6,7,5,3,2,1}
Duplicate elements : 5,3
sum of duplicate elements :5+3=8.
20.class Metti{
public static void main(String[] args)
{
int a[] =(10,20,3);
System.out.println(a.length);
}
}
21.class Metti{
public static void main(String[] args)
{
float f1=10.20f;
float f2=10.20f;
System.out.println(f1==f2);
}
5
ARRAY PROGRAMS
}
22.#include<stdio.h>
int main()
{
int arr[] = {12,13,14,15,16};
printf("%d,%d,%d\n",sizeof(arr),sizeof(*arr),sizeof(arr[0]))
return 0;
}
a)10,2,4
b)20,4,4
c)16,2,2
d)20,2,2
23.#include<stdio.h>
int main()
{
Static char *s[] = {"black","White","pink","violet");
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s",**p+1);
return 0;
}
6
ARRAY PROGRAMS
24.Given 2 arrays,find which numbers from first array is not present in second
array,Eg[1,4,2,5,6] and [3,6,2,7,9] ans-1,4,5.
26.Write a program to find largest and smallest numbers in an given unsorted array.
29.Write a program to find out if there are any consecutive numbers in a given array,
and print the sequence
example- i/p:{7,4,6,9,1,2,3,4,30,40,32}
o/p:{1,2,3,4}
30.Write a program to read the age of a few persons.and then based on their age,print
count of childern and adults?
example- i/p:{10,20,30,40,15,8,50,60}
o/p: Children count:3
Adult count: 5
7
ARRAY PROGRAMS
31.Write a program to reverse a given array by swapping the elements within it,
without using a second array and without printing it from last to first
example-if user enters the i/p array A as{10,20,30,40,50,60,70}
when you print it o/p should be {70,60,50,40,30,20,10}
32.Program
public class Program12 {
public static void main(String[] args) {
int [] x= {10,20,70,60,40};
int highest=x[0];
int lowest=x[0];
for(int i:x) {
if(i>highest) {
highest = i;
}
if(i<lowest) {
lowest=i;
}
}
System.out.println(highest + " is highest number ");
System.out.println(lowest + " is lowest number ");
}
}
8
ARRAY PROGRAMS
33.Write a program for the below requirements.
Input :{-3,-2,-1,3,1};
Output:{-3,-2,-1,1,3};
Without changing the place of negative number we need to change the positions of
the positive number.
Test case2:
I/p a[] = {-6,4,2,-1,9};
o/p a[] = {-6,2,4,-1,9};
1.Write a program to print 2nd largest element in an array.