(Arrays)
1. Input n number of numbers in an array
import [Link].*;
class array
public static void main()
Scanner ob=new Scanner([Link]);
[Link]("ḧow many numbers do u want to enter");
int n=[Link]();
int a[]=new int[n];
int i;
for(i=0;i<n;i++)
a[i]=[Link]();
}
2. Input n names in an array and print all the names entered
import [Link].*;
class array
public static void main()
Scanner ob=new Scanner([Link]);
[Link]("ḧow many names do u want to enter");
int n=[Link]();
String a[]=new String[n];
int i;
for(i=0;i<n;i++)
a[i]=[Link]();
for(i=0;i<n;i++)
[Link](a[i]);
}
3. Input percentage obtained by 10 students, print the class average
import [Link].*;
class array
public static void main()
Scanner ob=new Scanner([Link]);
double a[]=new double[10];
int i;
double sum=0;
double avg;
for(i=0;i<[Link];i++)
[Link]("enter ur percentage");
a[i]=[Link]();
sum = sum+a[i];
avg = sum /10.0;
[Link](" Average percentage "+ avg);
}
4. An array contains 10 characters , count how many of them are vowels
import [Link].*;
class array2
public static void main()
Scanner ob=new Scanner([Link]);
char a[]=new char[10];
int i,v=0;
for(i=0;i<[Link];i++)
a[i]=[Link]().charAt(0);
for(i=0;i<[Link];i++)
a[i] = [Link](a[i]);
if(a[i] =='A' || a[i] == 'E'|| a[i] =='I' || a[i] == 'O' || a[i] =='U')
v++;
[Link]("vowels"+v);
}
#INCLUDE - 90047 74268
5. Input 10 numbers in an array. Store the elements of the array in reverse order in another array.
Print both arrays side by side
import [Link].*;
class race
public static void main()
Scanner ob = new Scanner([Link]);
int arr[] = new int[10];
int rev[] = new int[10];
int i;
[Link]("enter 10 nos");
for(i=0;i<10;i++)
arr[i] = [Link]();
int k=9;
for(i=0;i<10; i++)
rev[i] = arr[k];
k--;
for(i=0;i<10;i++)
[Link](rev[i]);
}
6. Input 10 numbers in an Array, Replace all the odd numbers with the previous even number closest to it.
Example – if a number stored in an element is 9 it should be changed to 8
import [Link].*;
class arrays
public static void main()
Scanner ob = new Scanner([Link]);
int i;
int a[] = new int[10];
for(i=0;i<10;i++)
[Link]("enter a number");
a[i]=[Link]();
for(i=0;i<10;i++)
if(a[i]%2!=0)
a[i]=a[i]-1;
for(i=0;i<10;i++)
[Link](a[i]);
}
7. Input 10 numbers in an array. Print the smallest and the largest number.
import [Link].*;
class array5
public static void main()
Scanner ob=new Scanner([Link]);
int i, max = 0, min=9;
int a[] = new int[10];
for(i=0;i<10;i++)
[Link]("Enter 10 numbers");
a[i]=[Link]();
for(i=0;i<10;i++)
if(a[i] > max)
max=a[i];
for(i=0;i<10;i++)
if(a[i]<min)
min=a[i];
[Link]("the smallest number is" + min);
[Link]("the biggest number is" + max);
}
8. Array A contains 4 numbers – 10,20,30,40. Array B contains 3 numbers 7,8,9. Create a new Array C which
stores numbers from both the arrays
class array10
public static void main()
int a[] = {10,20,30,40};
int b[] = {7,8,9};
int c[] = new int[7];
int i,j;
for(i=0; i<[Link]; i++)
c[i] = a[i];
for(i=0; i<[Link]; i++)
c[i+[Link]] = b[i];
for(i=0; i<[Link]; i++)
[Link](c[i]);
}
9. Store 10 numbers in an array. Input a number from the user and check if the number is present in the array
using Linear Search Method.
import [Link].*;
class linearsearch
public static void main()
Scanner ob=new Scanner([Link]);
int a[]={1,2,4,5,6,7,8,9,10};
int c=-1,i;
int n;
[Link]("enter no to search");
n=[Link]();
for(i=0; i<5; i++)
if(a[i]==(n))
c=i+1;
if(c>-1)
[Link]("the no is present at " + c +"position");
else
[Link]("no is absent");
}
#INCLUDE - 90047 74268
}
10. Store 10 numbers in an array. Input a number from the user and check if the number is present in the array
using Binary Search Method.
import [Link].*;
class binarysearch
public static void main()
Scanner ob=new Scanner ([Link]);
int n; boolean ans=false;
[Link]("enter number to search ");
n=[Link]();
int a[]={ 1,2,3,4,5,6,7,8,9,10}
int start=0;
int end=[Link]-1;
while(start<=end)
int middle=(start+end)/2;
if(a[middle]==n)
pos=mid;
break;
else if (n>a[middle])
start=middle+1;
else
end=middle+1;
if(pos==-1)
[Link]("Not found");
else
[Link]("Found");
}
11. Input 10 numbers is an array. Sort the array using bubble sort and print the sorted array.
import [Link].*;
class bubblesort
public static void main()
Scanner ob=new Scanner([Link]);
int a[]=new int [10];
int temp;
int i,j,s=0;
[Link]("enter ten numbers");
for(i=0 ; i<10 ; i++)
a[i]=[Link]();
for(i=0 ; i<[Link]-1 ;i++) //no of rounds
for(j=0 ; j<[Link]-1-i ; j++)
if(a[j] < a[j+1])
temp = a[j] ;
a[j] = a[j+1];
a[j+1] = temp;
for(i=0 ; i<10 ; i++)
[Link](a[i]);
}
12. Store 10 numbers is an array. Sort the array using selection sort and print the sorted array.
class selection1234
public static void main()
int a[]= { 34 , 12 , 9, 54,67,7,2,4,8,4};
int i,j,temp=0;
for(i=0;i<10;i++)
for(j=i+1; j<10 ; j++)
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<[Link];i++)
[Link](a[i]+ " ");
}
#INCLUDE - 90047 74268
13. Store 10 numbers in an array and sort them using exchange selection sort method.
import [Link].*;
class exSelectionSort1
{
public static void main()
{
int n[] = new int[10];
int x,y;
Scanner ob=new Scanner([Link]);
for (int i = 0; i < 10; i++) {
n[i] = [Link]();
}
for (int i = 0; i < 10; i++)
{
int min = n[i];
int minpos = i;
for (int j = i + 1; j < 10; j++)
{
if ( n[j] < min )
{
min = n[j];
minpos = j;
}
}
x=n[i];
y=n[minpos];
n[i] = y;
n[minpos] = x;
}
[Link]("Output : ");
for (int i = 0; i < 10; i++)
{
[Link](n[i]);
}
}
}