0% found this document useful (0 votes)
2 views142 pages

Array Program Book Final

The document contains Java programs that demonstrate various operations on arrays, including printing elements, copying arrays, finding odd and even elements, identifying duplicates, and calculating sums and averages. Each program is structured with user input for array size and elements, followed by specific functionality such as printing in reverse order or searching for elements. The document serves as a comprehensive guide for mastering array manipulation in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views142 pages

Array Program Book Final

The document contains Java programs that demonstrate various operations on arrays, including printing elements, copying arrays, finding odd and even elements, identifying duplicates, and calculating sums and averages. Each program is structured with user input for array size and elements, followed by specific functionality such as printing in reverse order or searching for elements. The document serves as a comprehensive guide for mastering array manipulation in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 142

Mastering

Array
Question

PRINT ALL THE ELEMENTS IN AN ARRAY.


Answer

import java.util.Scanner;
public class ElementsInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing elements in an array */


System.out.println("Elements in an array");
for(i=0;i< n;i++)
{
System.out.print(arr[i]+"");
}

}
}
Question

COPY ALL THE ELEMENTS OF ONE ARRAY


INTO ANOTHER ARRAY.
Answer

import java.util.Scanner;
public class copyArraytoanotherArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;
int[] arr2;
int n=0,i=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}

//Create another array arr2 with size of arr1


arr2 = new int[arr1.length];

//Copying all elements of one array into another


for ( i = 0; i < arr1.length; i++)
{
arr2[i] = arr1[i];
}

/*Displaying elements of array arr1*/


System.out.println("Elements of original array: ");
for ( i = 0; i < arr1.length; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println();

/*Displaying elements of array arr2 */


System.out.println("Elements of new array: ");
for ( i = 0; i < arr2.length; i++)
{
System.out.print(arr2[i] + " ");
}
}
}
Question

PRINT ODD ELEMENTS IN AN ARRAY.


Answer

import java.util.Scanner;
public class oddElementsInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing odd elements in an array */


System.out.println("Odd Elements in an array");
for(i=0;i< n;i++)
{
if(arr[i]%2!=0)
{
System.out.print(arr[i]+"");
}
}

}
}
Question

PRINT EVEN ELEMENTS IN AN ARRAY.


Answer

import java.util.Scanner;
public class evenElementsInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing even elements in an array */


System.out.println("Even Elements in an array");
for(i=0;i< n;i++)
{
if(arr[i]%2==0)
{
System.out.print(arr[i]+"");
}
}

}
}
Question

PRINT ELEMENTS IN AN ARRAY THAT


ARE IN ODD POSITIONS.
Answer

import java.util.Scanner;
public class ElementsInOddPositions
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing elements of an array in odd positions */


System.out.println("Elements of an array in odd positions");
for(i=0;i< n;i++)
{
if(i%2!=0)
{
System.out.print(arr[i]+"");
}
}
}
}
Question

PRINT ELEMENTS IN AN ARRAY THAT


ARE IN EVEN POSITIONS.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION


ELEMENTS:
2 4 6 8 10
Answer

import java.util.Scanner;
public class ElementsInEvenPositions
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing elements of an array in even positions */


System.out.println("Elements of an array in even positions");
for(i=0;i< n;i++)
{
if(i%2==0)
{
System.out.print(arr[i]+"");
}
}
}
}
Question

PRINT DUPLICATE ELEMENTS OF AN


ARRAY.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION


ELEMENTS:
2 4 6 8 10
Answer

import java.util.Scanner;
public class duplicateNumbersInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

System.out.println("Duplicate numbers in array are:");

/*finding the frequency of elements the array and printing the number and frequency
of it */
for ( i = 0; i < n; i++)
{
count=0;
if(arr[i]!=-9999999)
{
for(j=0;j< n;j++)
{
if(arr[j]==arr[i])
{
/* replacing the duplicate element so that it is not repeated again */
arr[j]=-9999999;
count++;

}
}
if(count>1)
{
System.out.println(i+"");
}
}

}
}
}
Question

PRINT ARRAY ELEMENTS IN REVERSE


ORDER.
Answer

import java.util.Scanner;
public class arrayElementsinReverseOrder
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr=new int[n];
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Original array: ");
for ( i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}

System.out.println();

System.out.println("Array in reverse order: ");


/* Loop through the array in reverse order */
for (i = arr.length-1; i >= 0; i--)
{
System.out.print(arr[i] + " ");
}
}
}
Question

COPY ALL THE ELEMENTS OF ONE ARRAY


INTO ANOTHER ARRAY IN REVERSE
ORDER.
Answer

import java.util.Scanner;
public class copyArraytoanotherArrayInReverseOrder
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;
int[] arr2;
int n=0,i=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}

//Create another array arr2 with size of arr1


arr2 = new int[arr1.length];
int j=arr1.length-1;
//Copying all elements of one array into another in reverse order
for ( i = 0; i < arr1.length; i++)
{
arr2[i] = arr1[j];
j--;
}

/*Displaying elements of array arr1*/


System.out.println("Elements of original array: ");
for ( i = 0; i < arr1.length; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println();

/*Displaying elements of array arr2 */


System.out.println("Elements of array in reverse order: ");
for ( i = 0; i < arr2.length; i++)
{
System.out.print(arr2[i] + " ");
}
}
}
Question

WRITE A PROGRAM TO PRINT THE


LARGEST ELEMENT IN AN ARRAY.
Answer

import java.util.Scanner;
public class largestElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,max=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
max=arr1[0];

System.out.println();

for ( i = 0; i < n; i++)


{
if(arr1[i]> max)
{
max=arr1[i];
}
}
System.out.println("Largest element in the array is "+max);
}
}
Question

WRITE A PROGRAM TO PRINT THE


SMALLEST ELEMENT IN AN ARRAY.
Answer

import java.util.Scanner;
public class smallestElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,min=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
min=arr1[0];

System.out.println();

for ( i = 0; i < n; i++)


{
if(arr1[i]< min)
{
min= arr1[i];
}
}
System.out.println("Smallest element in the array is "+min);
}
}
Question

WRITE A PROGRAM TO PRINT THE SUM


OF ALL ELEMENTS IN AN ARRAY.
Answer

import java.util.Scanner;
public class sumOfElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,sum=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println();
for ( i = 0; i < n; i++)
{
sum=sum+arr1[i];
}
System.out.println("Sum of all element in the array is "+sum);
}
}
Question

PRINT THE AVERAGE OF ALL ELEMENTS


OF AN ARRAY.
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION


ELEMENTS:
2 4 6 8 10
Answer

import java.util.Scanner;
public class averageOfElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
double arr[];
double sum=0;
int size=0,i=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


size = sc.nextInt();
System.out.println("Enter numbers in array");
arr=new double[size];
for(i=0;i< size;i++)
{
arr[i]=sc.nextDouble();
}
System.out.println("Element entered in given array");

for(i=0;i< size;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();

for ( i = 0; i < size; i++)


{
sum=sum+arr[i];
}
double average=sum/size;
System.out.println("Average of all element in the array is "+average);
}
}
Question

PRINT FREQUENCY OF EACH ELEMENT


OF AN ARRAY.
Answer

import java.util.Scanner;
public class frequencyOfNumbersInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number of elements in the array");
n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

System.out.println("---------------------");
System.out.println(" ElementFrequency");
System.out.println("---------------------");

/*finding the frequency of elements the array and printing the number and frequency
of it */
for (i = 0; i < n; i++)
{
count=0;
if(arr[i]!=-9999999)
{
for(j=0;j< n;j++)
{
if(arr[j]==arr[i])
{
/* replacing the duplicate element so that it is not repeated again */
arr[j]=-9999999;
count++;

}
}
System.out.println(i+""+count);

}
}
Question

SEARCH AN ELEMENT IN AN ARRAY.


Answer

import java.util.Scanner;
public class searchElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;
boolean elementfound=false;
int n=0,i=0,item=0,pos=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Enter element to be searched in an array");
item=sc.nextInt();
System.out.println();

for ( i = 0; i < n; i++)


{
if(arr1[i]==item)
{
elementfound=true;
pos=i;
break;
}
}
if(elementfound==true)
{
System.out.println(item+" found in the array at position "+pos);
}
else
{
System.out.println(item+" notfound in the array.");
}
}
}
Question

PRINT POSITIONS OF ALL DUPLICATE


ELEMENTS IN AN ARRAY TO BE
SEARCHED.
EXAMPLE:
ARR1={1,2,1,3,1,4,5}
ELEMENTS TO BE SEARCHED:1
OUTPUT :1
FOUND AT POSITIONS:
0
2
4
Answer

import java.util.Scanner;
public class positionsOfDuplicateElementsToBeSearched
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,item=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no. of elements in the array");
n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Enter element to be searched in an array");
item=sc.nextInt();
System.out.println();
System.out.println(item+" found at position/positions:" );

for ( i = 0; i < n; i++)


{
if(arr1[i]==item)
{
System.out.println(i);
}
}

}
}
Question

PRINT THE SECOND LARGEST NUMBER


IN AN ARRAY.
Answer

import java.util.Scanner;
public class secondLargestElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,max=0,sl=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no. of elements in the array");
n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
max=arr1[0];
sl=arr1[0];
System.out.println();

for ( i = 0; i < n; i++)


{
if(arr1[i]> max)
{
max=arr1[i];
}
if(arr1[i]>sl && arr1[i]< max)
{
sl=arr1[i];
}
}
System.out.println("Second Largest element in the array is "+sl);
}
}
Question

PRINT SECOND SMALLEST IN AN ARRAY.


Answer

import java.util.Scanner;
public class secondSmallestElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,min=0,ss=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
min=arr1[0];
ss=arr1[0];
System.out.println();

for ( i = 0; i < n; i++)


{
if(arr1[i]< min)
{
min=arr1[i];
}
if(arr1[i]< ss && arr1[i]> min)
{
ss=arr1[i];
}
}
System.out.println("Second Smallest element in the array is "+ss);
}
}
Question

WRITE A PROGRAM TO SORT AN ARRAY


IN ASCENDING ORDER.
Answer

import java.util.Scanner;
public class sortArrayInAsc
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,j=0,temp=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no. of elements in the array");
n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Elements of original array: ");
for (i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}

/* sorting of array in ascending order is done here */


for (i = 0; i < n; i++)
{
for (j = i+1; j < n; j++)
{
if(arr1[i] > arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}

System.out.println();

/* Sorted array */
System.out.println("Elements of array in ascending order: ");
for (i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}
}
}
Question

WRITE A PROGRAM TO SORT ARRAY IN


DESCENDING ORDER.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION


ELEMENTS:
2 4 6 8 10
Answer

import java.util.Scanner;
public class sortArrayInDesc
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,j=0,temp=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Elements of original array: ");
for (i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}

/* sorting of array in descending order is done here */


for ( i = 0; i < n; i++)
{
for (j = i+1; j < n; j++)
{
if(arr1[i] < arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
System.out.println();
/* Sorted array */
System.out.println("Elements of array in descending order: ");
for (i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}
}
}
Question

WRITE A PROGRAM TO K TH LARGEST


ELEMENT IN AN ARRAY.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION


ELEMENTS:
2 4 6 8 10
Answer

import java.util.Scanner;
public class kthLargestElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,j=0,temp=0,k=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Elements of original array: ");
for ( i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println("Enter k th position.(position should be between 0 and "+n+")");
k=sc.nextInt();
/* sorting of array in descending order is done here */
for (i = 0; i < n; i++)
{
for ( j = i+1; j < n; j++)
{
if(arr1[i] < arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
System.out.println();

/* k th element */
if(k>0 && k< n)
{
System.out.println(k+" largest element of the given array is: "+arr1[k]);
}
else
{
System.out.println("invalid value of k");
}
}
}
Question

WRITE A PROGRAM TO K TH SMALLEST


ELEMENT IN AN ARRAY.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION


ELEMENTS:
2 4 6 8 10
Answer

import java.util.Scanner;
public class kthSmallestElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,j=0,temp=0,k=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Elements of original array: ");
for (i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println("Enter k th position.(position should be between 0 and "+n+")");
k=sc.nextInt();
/* sorting of array in ascending order is done here */
for ( i = 0; i < n; i++)
{
for ( j = i+1; j < n; j++)
{
if(arr1[i] > arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}

System.out.println();

/* k th element */
if(k>0 && k< n)
{
System.out.println(k+" smallest element of the given array is: "+arr1[k]);
}
else
{
System.out.println("invalid value of k");

}
}
}
Question

PRINT LARGEST AND SMALLEST


ELEMENT IN AN UNSORTED ARRAY.
Answer

import java.util.Scanner;
public class largestAndSmallestElementInAnUnsortedArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr1;

int n=0,i=0,j=0,temp=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a no. of elements in the array");


n = sc.nextInt();
System.out.println("Enter numbers in array");
arr1=new int[n];
for(i=0;i< n;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Elements of original array: ");
for (i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}

/* sorting of array in descending order is done here */


for ( i = 0; i < n; i++)
{
for ( j = i+1; j < n; j++)
{
if(arr1[i] < arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
System.out.println();
System.out.println("Largest Elements of array: "+arr1[0]);
System.out.println("Smallest Elements of array: "+arr1[(n-1)]);

}
}
Question

WRITE A PROGRAM TO SORT AN


UNSORTED ARRAY USING SELECTION OR
LINEAR SORT.
Answer

import java.util.Scanner;
public class SelectionSortArray
{
public static void main(String args[])
{
int n=0,i=0,j=0,temp=0;
int[] arr;
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of elements in the array");
n = sc.nextInt();

System.out.println("Enter numbers in array");


arr=new int[n];
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

System.out.println("Unsorted Array:");
for (i=0; i< n; ++i)
{
System.out.print(arr[i] + " ");
System.out.println();
}

/*Sorting of array*/
for (i = 0; i < n-1; i++)
{
for (j = i+1; j < n; j++)
{
if (arr[i] > arr[j+1])
{
// swap arr[j+1] and arr[i]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

System.out.println("Sorted Array:");
for (i=0; i< n; i++)
{
System.out.print(arr[i] + " ");
System.out.println();
}

}
}
Question

WRITE A PROGRAM TO SORT AN ARRAY


USING BUBBLE SORT.
Answer

import java.util.Scanner;
public class BubbleSortArray
{
public static void main(String args[])
{
int[] arr;
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of elements in the array");
int n = sc.nextInt();

System.out.println("Enter numbers in array");


arr=new int[n];
for(int i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

System.out.println("Unsorted Array:");
for (int i=0; i< n; ++i)
{
System.out.print(arr[i] + " ");
System.out.println();
}

/*Sorting of array*/
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

System.out.println("Sorted Array:");
for (int i=0; i< n; ++i)
{
System.out.print(arr[i] + " ");
System.out.println();
}

}
}
Question

WRITE A PROGRAM TO PRINT PRIME NUMBER IN AN ARRAY


PRIME NUMBER IS A NUMBER THAT IS GREATER THAN 1 AND
DIVIDED BY 1 OR ITSELF ONLY. IN OTHER WORDS, PRIME
NUMBERS CAN'T BE DIVIDED BY OTHER NUMBERS THAN
ITSELF OR 1. FOR EXAMPLE 2, 3, 5, 7, 11, 13, 17.... ARE THE
PRIME NUMBERS.

ENTER SIZE OF LIST:10


ENTER ELEMENTS IN LIST:
1:10
2:13
3:14
4:-10
5:-13
6:1
7:98
8:23
9:24
10:2
ELEMENTS ENTERED IN GIVEN LIST:
[10, 13, 14, -10, -13, 1, 98, 23, 24, 2]
PRIME NUMBERS IN GIVEN LIST:
13 23 2
Answer

import java.util.Scanner;
public class PrimeNumberInGivenArray
{
public static void main(String args[])
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0;
boolean flag=true;

Scanner sc = new Scanner(System.in);


System.out.println("Enter size of array:");
n = sc.nextInt();
arr=new int[n];

System.out.println("Enter numbers in array:");


for(i=0;i< n;i++)
{
System.out.print((i+1)+":");
arr[i]=sc.nextInt();
}

System.out.println("Elements Entered in given array:");


for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("prime Elements in an array:");
for(i=0;i< n;i++)
{

flag=true;
if(arr[i]>1)
{
for(j=2;j< arr[i];j++)
{
/*Checking if number is divisible by j.*/
if(arr[i]%j==0)
{
flag=false;
break;
}
}
if(flag==true)
{
System.out.print(arr[i]+" ");
}
}
}

}
}
Question

PRINT PALINDROME NUMBER IN AN ARRAY.


INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION ELEMENTS:


2 4 6 8 10
Answer

import java.util.Scanner;
public class PalindromeNumberInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,count=0,remainder=0,temp=0,reversedInteger=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing palindrome elements in an array */


System.out.println("palindrome Elements in an array");
for(i=0;i< n;i++)
{
temp=arr[i];
reversedInteger=0;
// reversed integer is stored in variable
while( temp != 0 )
{
remainder = temp % 10;
reversedInteger = reversedInteger * 10 + remainder;
temp /= 10;
}

/* palindrome if n and reversedInteger are equal*/


if (arr[i] == reversedInteger)
{
System.out.println(n + "");
}

}
}
Question

PRINT DISARIUM NUMBER IN AN ARRAY.


(A NUMBER IS SAID TO BE THE DISARIUM NUMBER WHEN
THE SUM OF ITS DIGIT RAISED TO THE POWER OF THEIR
RESPECTIVE POSITIONS IS EQUAL TO THE NUMBER ITSELF.)
11 + 72 + 53 = 1 + 49 + 125 = 175
Answer

import java.util.Scanner;
public class DisariumNumber
{
public static void main(String[] args)
{
int arr[];
int num = 0, sum = 0, rem = 0, temp=0,len=0,size=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of Array:");
size=sc.nextInt();
arr=new int[size];
System.out.println("Enter Elements in Array:");
for(i=0;i< size;i++)
{
System.out.print((i+1)+":");
arr[i]=sc.nextInt();
}
System.out.println("Elements in Given Array:");
for(i=0;i< size;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("Disarium number in given Array:");
for(i=0;i< size;i++)
{
temp = arr[i];
sum=0;
// To find the length of the number
while(temp>0)
{
len= len + 1;
temp= temp/10;
}
/*Makes a copy of the original number num*/
temp = arr[i];

/*Calculates the sum of digits powered with their respective position */


while(temp > 0)
{
rem = temp%10;
sum = sum + (int)Math.pow(rem,len);
temp = temp/10;
len--;
}

/*Checks whether the sum is equal to the number itself*/


if(sum == arr[i])
{
System.out.print(arr[i] + " ");
}

}
}
Question

PRINT ARMSTRONG NUMBER IN AN ARRAY.

A POSITIVE NUMBER IS CALLED ARMSTRONG NUMBER IF SUM OF ITS


OWN DIGITS EACH RAISED TO THE POWER OF THE NUMBER OF DIGITS IS
EQUAL TO NUMBER
153
1^3+5^3+3^3=1+125+27=153
153 IS AN ARMSTRONG NUMBER

ENTER THE SIZE OF LIST:10


ENTER ELEMENT IN GIVEN LIST:
1:30
2:20
3:1
4:153
5:99
6:107
7:19
8:145
9:131
10:3
ELEMENTS IN GIVEN LIST:
[30, 20, 1, 153, 99, 107, 19, 145, 131, 3]
ARMSTRONG NUMBER IN GIVEN LIST:
1 153 3
Answer

import java.util.Scanner;
public class ArmstrongNumberInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,temp=0,c=0,r=0,noOfDigits=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Elements entered in Given array");

for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();

/* printing Armstrong elements in an array */


System.out.println("Armstrong Elements in an array:");
for(i=0;i< n;i++)
{
c=0;
temp=arr[i];
noOfDigits=Integer.toString(temp).length();
while(temp>0)
{
r=temp%10;
temp=temp/10;
c=c+(int)(Math.pow(r,noOfDigits));
}
if(arr[i]==c)
{
System.out.print(arr[i]+" ");
}
}

}
}
Question

PRINT NEON NUMBER IN AN ARRAY.


A NEON NUMBER IS A NUMBER WHERE THE SUM OF DIGITS
OF SQUARE OF THE NUMBER IS EQUAL TO THE NUMBER. FOR
EXAMPLE IF THE INPUT NUMBER IS 9, ITS SQUARE IS 9*9 =
81 AND SUM OF THE DIGITS IS 9. I.E. 9 IS A NEON NUMBER.
Answer

import java.util.Scanner;
public class NeonNumberInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,squaredNumber=0,a=0,sum=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enternumber of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing Neon elements in an array */


System.out.println("Neon Elements in an array");
for(i=0;i< n;i++)
{
squaredNumber=arr[i]*arr[i];
sum=0;
/*Loop to find the sum of digits.*/
while(squaredNumber!=0)
{
a=squaredNumber%10;
sum=sum+a;
squaredNumber=squaredNumber/10;
}
if(sum==arr[i])
{
System.out.println(arr[i]+" ");
}

}
}
Question

PRINT PRONIC NUMBER IN AN ARRAY.


(THE PRONIC NUMBER CAN BE DEFINED AS THE NUMBER
WHICH IS A PRODUCT OF TWO CONSECUTIVE NUMBERS. )
Answer

import java.util.Scanner;
public class PronicNumberInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0;
boolean flag=false;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing pronic elements in an array */


System.out.println("pronic Elements in an array :");
for(i=0;i< n;i++)
{
flag=false;
for(j=0;j<=arr[i];j++)
{
if((j*(j+1))==arr[i])
{
flag=true;
break;
}
}
if(flag==true)
{
System.out.print(arr[i] + " ");
}

}
}
}
Question

PRINT HAPPY NUMBER IN AN ARRAY.

(A NUMBER IS CALLED HAPPY IF IT LEADS TO 1 AFTER A SEQUENCE OF


STEPS WHEREIN EACH STEP NUMBER IS REPLACED BY THE SUM OF
SQUARES OF ITS DIGIT THAT IS IF WE START WITH HAPPY NUMBER AND
KEEP REPLACING IT WITH DIGITS SQUARE SUM, WE REACH 1.)

INPUT: N = 19
OUTPUT: TRUE
19 IS HAPPY NUMBER,
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
AS WE REACHED TO 1, 19 IS A HAPPY NUMBER.
Answer

import java.util.Scanner;
public class HappyNumberInArray
{
public static int SumOfSquareOfDigits (int number)
{
int rem = 0, sum = 0;

while(number > 0)
{
rem = number %10;
sum = sum+(rem*rem);
number = number/10;
}
return sum;
}
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,result=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
/* printing Happy elements in an array */
System.out.println("happy Elements in an array");
for(i=0;i< n;i++)
{
if(arr[i]>0)
{
result = arr[i];
while (result != 1 && result != 4)
{
result = SumOfSquareOfDigits(result);
}
if (result ==1)
{
System.out.println (arr[i]+"");
}
}

}
}
}
Question

WRITE A PROGRAM TO PRINT UNIQUE NUMBER(S) IN AN


ARRAY.

(A UNIQUE NUMBER IS A POSITIVE INTEGER (WITHOUT LEADING


ZEROS) WITH NO DUPLICATE DIGITS. FOR EXAMPLE 7, 135, 214 ,
5243 ARE ALL UNIQUE NUMBERS WHEREAS 33, 3121, 200 ARE NOT.)
Answer

import java.util.Scanner;
public class UniqueNumberInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,flag=0,count=0,r=0,temp=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing unique elements in an array */


System.out.println("unique Elements in an array");
for(i=0;i< n;i++)
{
for(j=0;j<=9;j++)
{
temp=arr[i];
count=0;
flag=0;
while(temp>0)
{
r=temp%10;
if(r==j)
/* if any digits are repeated, then it is not a UniqueNumber*/

{
count=count+1;
}
temp=temp/10;
}
if(count>1)
{

flag=1;
break;
}

if(flag ==0)
{
System.out.println(arr[i]+" ");
}

}
}
Question
WRITE A PROGRAM TO PRINT REVERSE OF NUMBERS IN AN ARRAY.

ENTER A NUMBER OF ELEMENTS IN THE ARRAY


5
ENTER NUMBERS IN ARRAY
10
35
31
19
116
REVERSE NUMBERS IN AN ARRAY
1 53 13 91 611
Answer

import java.util.Scanner;
public class ReverseNumbersInArray
{

public static void main(String[] args)


{
/* Initialize array */
int [] arr;

int n=0,i=0,temp=0,reversedInteger=0,remainder=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number of elements in the array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing reverse numbers in an array */


System.out.println("reverse numbers in an array");
for(i=0;i< n;i++)
{
reversedInteger=0;
temp= arr[i];
/* reversed integer is stored in variable */
while( temp != 0 )
{
remainder = temp % 10;
reversedInteger = reversedInteger * 10 + remainder;
temp /= 10;
}
System.out.print(reversedInteger+" ");
}

}
}
Question
WRITE A PROGRAM TO DETERMINE WHETHER A GIVEN NUMBER IS A
HARSHAD NUMBER.

(A NUMBER IS SAID TO BE THE HARSHAD NUMBER IF IT IS


DIVISIBLE BY THE SUM OF ITS DIGIT.)
Answer

import java.util.Scanner;
public class HarshadNumberInArray
{

public static void main(String[] args)


{
/* Initialize array */
int [] arr;

int n=0,i=0,sum=0,rem=0,temp=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number of elements in the array");
n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

/* printing Harshad elements in an array */


System.out.println("Harshad Elements in an array");
for(i=0;i< n;i++)
{
/*Make a copy of arr[i] and store it in variable temp*/
temp = arr[i];
sum=0;
/*Calculates sum of digits*/
while(temp> 0)
{
rem = temp%10;
sum = sum + rem;
temp = temp/10;
}
//Checks whether number is divisible by sum of digits
if((arr[i]%sum) == 0)
{
System.out.println(arr[i]+ "");
}
}
}
}
Question
PRINT TWISTED PRIME NUMBER IN AN ARRAY.

(A NUMBER IS CALLED A TWISTED PRIME NUMBER IF IT IS A PRIME


NUMBER AND REVERSE OF THIS NUMBER IS ALSO A PRIME
NUMBER.)
Answer

import java.util.Scanner;
public class TwistedPrimeInGivenArray
{
/* reverseOfNumber() reverses a number */
public static int reverseOfNumber(int num)
{
int temp=0,reversedInteger=0,remainder=0;
temp=num;
while( temp > 0 )
{
remainder = temp % 10;
reversedInteger = reversedInteger * 10 + remainder;
temp /= 10;
}
return reversedInteger;
}

/* isPrime() checks whether a number is prime number or not */


public static boolean isPrime(int num)
{
boolean flag = true;

if(num>1)
{
for (int j = 2; j <= num / 2; j++)
{
if ((num % j) == 0)
{
flag = false;
break;
}
}
}
else
{
flag=false;
}
return flag;
}

public static void main(String[] args)


{

int reversedNumber=0,size=0;
int arr[];
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array:");
size = sc.nextInt();
arr=new int[size];
System.out.println("Enter elements in Array:");
for(int i=0;i<size;i++)
{
arr[i] = sc.nextInt();
}
System.out.println("Twisted Prime numbers in given array:");
for(int i=0;i<size;i++)
{
if (isPrime(arr[i])==true)
{
reversedNumber=reverseOfNumber(arr[i]);
if(isPrime(reversedNumber)==true)
{
System.out.print(arr[i]+" ");
}
}

}
}
Question
DELETE EVEN ELEMENT FROM THE GIVEN ARRAY.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN ELEMENTS:


13579
Answer

import java.util.Scanner;
public class DeleteEvenElementFromArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,k=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

for(i=0;i< n;i++)
{
if(arr[i]%2==0)
{
k=i;
for(j=k+1;j< n;j++)
{
arr[k]=arr[j];
k++;
}
i=0;
n=n-1;
}
}
System.out.println("array after deleting even element");

for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
WRITE A PROGRAM TO DELETE ELEMENT FROM THE
GIVEN ARRAY.

ENTER SIZE OF ARRAY


5
ENTER NUMBERS IN ARRAY
1:10
2:20
3:30
4:40
5:50
ELEMENTS GIVEN IN ARRAY:
10 20 30 40 50
ENTER POSITION TO BE DELETED:
3
ARRAY AFTER DELETING ELEMENT IN GIVEN POSITION
10 20 40 50
Answer

import java.util.Scanner;
public class DeleteElementFromGivenPositionArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int sizeOfArray=0,i=0,positionToBeDeleted=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


sizeOfArray = sc.nextInt();
arr=new int[sizeOfArray];
System.out.println("Enter numbers in array");

for(i=0;i< sizeOfArray;i++)
{
System.out.print((i+1)+":");
arr[i]=sc.nextInt();
}
System.out.println("Elements given in Array:");
for(i=0;i< sizeOfArray;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();

System.out.println("Enter Position to be deleted:");


positionToBeDeleted=sc.nextInt();
//We are Assuming that position given by user will start from 1
//but as indexing in array starts from zero so thats why we are
//Decreasing the value of positionToBeDeleted by 1
positionToBeDeleted=positionToBeDeleted-1;
if(positionToBeDeleted<0 ||positionToBeDeleted>=sizeOfArray)
{
System.out.println("Invalid position Entered");
}
else
{
for(i=positionToBeDeleted;i< sizeOfArray-1;i++)
{
arr[i]=arr[i+1];

}
sizeOfArray=sizeOfArray-1;

System.out.println("array after deleting element in given position");


for(i=0;i< sizeOfArray;i++)
{
System.out.print(arr[i]+" ");
}
}

}
}
Question
DELETE EVEN POSITION ELEMENT FROM ARRAY
Answer

import java.util.Scanner;
public class DeleteElementFromGivenPositionArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int sizeOfArray=0,i=0,positionToBeDeleted=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


sizeOfArray = sc.nextInt();
arr=new int[sizeOfArray];
System.out.println("Enter numbers in array");

for(i=0;i< sizeOfArray;i++)
{
System.out.print((i+1)+":");
arr[i]=sc.nextInt();
}
System.out.println("Elements given in Array:");
for(i=0;i< sizeOfArray;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();

System.out.println("Enter Position to be deleted:");


positionToBeDeleted=sc.nextInt();
//We are Assuming that position given by user will start from 1
//but as indexing in array starts from zero so thats why we are
//Decreasing the value of positionToBeDeleted by 1
positionToBeDeleted=positionToBeDeleted-1;
Question
DELETE EVEN POSITION ELEMENT FROM THE GIVEN
ARRAY.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING EVEN POSITION ELEMENTS:


2 4 6 8 10
Answer

import java.util.Scanner;
public class DeleteEvenPositionElementFromArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,k=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
for(i=0;i< n;i++)
{
if((i+1)%2==0)
{
arr[i]=-999998;
}
}

for(i=0;i< n;i++)
{
if(arr[i]==-999998)
{
k=i;
for(j=k+1;j< n;j++)
{
arr[k]=arr[j];
k++;
}
i=0;
n=n-1;
}
}

System.out.println("array after deleting even position");

for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
DELETE AN ODD ELEMENT FROM THE GIVEN ARRAY.

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING ODD ELEMENTS:


2 4 6 8 10
Answer

import java.util.Scanner;
public class DeleteOddElementFromArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,k=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}

for(i=0;i< n;i++)
{
if(arr[i]%2!=0)
{
k=i;
for(j=k+1;j< n;j++)
{
arr[k]=arr[j];
k++;
}
i=0;
n=n-1;
}
}
System.out.println("array after deleting odd element");

for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
DELETE ELEMENTS FROM ARRAY DIVISIBLE FROM THE
NUMBER GIVEN BY USER

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ENTER THE DIVISOR:


5

ARRAY AFTER DELETING ELEMENTS:


12346789
Answer

import java.util.Scanner;
public class DeleteElementFromArrayDivisibleByNumber
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,k=0,divisor=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the divisor");
divisor=sc.nextInt();
for(i=0;i< n;i++)
{
if(arr[i]%divisor==0)
{
k=i;
for(j=k+1;j< n;j++)
{
arr[k]=arr[j];
k++;
}
i=0;
n=n-1;
}
}
System.out.println("array after deleting even position");
for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}

}
Question
DELETE ODD POSITION ELEMENT FROM THE ARRAY

INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10

ARRAY AFTER DELETING ODD POSITION ELEMENTS:


13579
Answer

import java.util.Scanner;
public class DeleteOddPositionElementFromArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,k=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
for(i=0;i< n;i++)
{
if((i+1)%2!=0)
{
arr[i]=-999998;
}
}

for(i=0;i< n;i++)
{
if(arr[i]==-999998)
{
k=i;
for(j=k+1;j< n;j++)
{
arr[k]=arr[j];
k++;
}
i=0;
n=n-1;
}
}

System.out.println("array after deleting odd position");

for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
TAKE AN ELEMENT FROM THE USER AND DELETE ALL
THE DUPLICATES OF THE GIVEN ELEMENT FROM THE
GIVEN ARRAY.

ENTER THE SIZE OF LIST:10


ENTER ELEMENTS IN LIST:
1
2
1
1
3
4
1
5
6
1
ELEMENTS IN GIVEN LIST:
1211341561
ENTER THE ELEMENT TO BE DELETED:1
LIST AFTER DELETING ELEMENT:
23456
Answer

import java.util.Scanner;
public class DeleteSearchedElementFromArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;

int n=0,i=0,j=0,k=0,item=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");


n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");

for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the item to be deleted");
item= sc.nextInt();

for(i=0;i< n;i++)
{

if(arr[i]==item)
{
k=i;
for(j=k+1;j< n;j++)
{
arr[k]=arr[j];
k++;
}
i=-1;
n=n-1;
}
}

System.out.println("array after deleting searched element");

for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}

}
Question
SORT AN ARRAY USING INSERTION SORT IN ASCENDING
ORDER.

ENTER THE SIZE OF ARRAY


5
ENTER THE ELEMENTS IN ARRAY
2
5
10
8
1
SORTED ARRAY:
1 2 5 8 10
Answer

import java.util.Scanner;
public class InsertionSortAscendingOrder
{

public static void main()


{
Scanner sc=new Scanner(System.in);
int sizeOfArray=0,i=0,j=0,val=0;
int arr[];
System.out.println("Enter the size of array");
sizeOfArray=sc.nextInt();
System.out.println("Enter the Elements in Array");
arr=new int[sizeOfArray];
for(i=0;i< sizeOfArray;i++)
{
arr[i]=sc.nextInt();
}
for (i = 1; i < sizeOfArray; ++i)
{
val = arr[i];

for(j=i-1;j>=0;j--)
{
if(arr[j]>val)
{
arr[j+1]=arr[j];
}
//break out of loop
//if arr[j] is less than variable 'val' which has value of arr[i]
else
{
break;
}
}
arr[j+1]=val;

}
System.out.println("Sorted Array:");
for(i=0;i< sizeOfArray;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
SORT AN ARRAY USING INSERTION SORT IN
DESCENDING ORDER.

ENTER THE SIZE OF ARRAY


5
ENTER THE ELEMENTS IN ARRAY
2
5
10
8
1
SORTED ARRAY:
10 8 5 2 1
Answer

import java.util.Scanner;
public class InsertionSortDescendingOrder
{

public static void main()


{
Scanner sc=new Scanner(System.in);
int sizeOfArray=0,i=0,j=0,val=0;
int arr[];
System.out.println("Enter the size of array");
sizeOfArray=sc.nextInt();
System.out.println("Enter the Elements in Array");
arr=new int[sizeOfArray];
for(i=0;i< sizeOfArray;i++)
{
arr[i]=sc.nextInt();
}
for (i = 0; i < sizeOfArray; ++i)
{
val = arr[i];

for(j=i-1;j>=0;j--)
{
if(arr[j]< val)
{
arr[j+1]=arr[j];
}
//break out of loop
//if arr[j] is less than variable 'val' which has value of arr[i]

else
{
break;
}
}

arr[j+1]=val;
}
System.out.println("Sorted Array:");
for(i=0;i< sizeOfArray;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
SORT AN ARRAY USING QUICK SORT IN ASCENDING
ORDER.

ENTER THE LIMIT OF THE ARRAY:5


ENTER THE NUMBER:10
ENTER THE NUMBER:21
ENTER THE NUMBER:13
ENTER THE NUMBER:14
ENTER THE NUMBER:5
UNSORTED ARRAY
10 21 13 14 5
SORTED ARRAY
5 10 13 14 21
Answer

import java.util.Scanner;
public class QuickSortInAscendingOrder
{
int arr[];
int len=0;
int i=0;
public void accept()
{

Scanner sc=new Scanner(System.in);


System.out.print("Enter the Limit of the array:");
len=sc.nextInt();
arr=new int[len];

for(i=0;i< len;i++)
{
System.out.print("Enter the Number:");
arr[i]=sc.nextInt();

}
}

public int partition(int arr[],int lowerBound,int upperBound)


{
int pivot=arr[lowerBound];
int start=lowerBound;
int end=upperBound;
int temp=0;
while(start< end)
{
while(startpivot)
{
end--;
}
if(start< end)
{
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
}
else
{
temp=arr[lowerBound];
arr[lowerBound]=arr[end];
arr[end]=temp;

}
}
return end;
}

public void quickSort(int arr[],int lb,int ub)


{
if(lb< ub)
{
int location=partition(arr,lb,ub);
quickSort(arr,lb,location-1);
quickSort(arr,location+1,ub);
}
}

public void display()


{

for(i=0;i< len;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main()
{
QuickSortInAscendingOrder ob1=new QuickSortInAscendingOrder();
ob1.accept();
System.out.println("Unsorted Array");
ob1.display();

ob1.quickSort(ob1.arr,0,ob1.len-1);
System.out.println("Sorted Array");
ob1.display();
}
}
Question
SORT AN ARRAY USING QUICK SORT IN DESCENDING
ORDER.

ENTER THE LIMIT OF THE ARRAY:10


ENTER THE NUMBER:125
ENTER THE NUMBER:10
ENTER THE NUMBER:-10
ENTER THE NUMBER:12
ENTER THE NUMBER:1
ENTER THE NUMBER:-62
ENTER THE NUMBER:3
ENTER THE NUMBER:50
ENTER THE NUMBER:120
ENTER THE NUMBER:144
UNSORTED ARRAY
125 10 -10 12 1 -62 3 50 120 144
SORTED ARRAY
144 125 120 50 12 10 3 1 -10 -62
Answer

import java.util.Scanner;
public class QuickSortInDescendingOrder
{
int arr[];
int len=0;
int i=0;
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Limit of the array:");
len=sc.nextInt();
arr=new int[len];

for(i=0;i< len;i++)
{
System.out.print("Enter the Number:");
arr[i]=sc.nextInt();
}
}

public int partition(int arr[],int lowerBound,int upperBound)


{
int pivot=arr[lowerBound];
int start=lowerBound;
int end=upperBound;
int temp=0;
while(start< end)
{
while(start< len && arr[start]>=pivot)
{
start++;
}
while(arr[end]< pivot)
{
end--;
}
if(start< end)
{
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
}
else
{
temp=arr[lowerBound];
arr[lowerBound]=arr[end];
arr[end]=temp;
}
}
return end;
}

public void quickSort(int arr[],int lb,int ub)


{
if(lb< ub)
{
int location=partition(arr,lb,ub);
quickSort(arr,lb,location-1);
quickSort(arr,location+1,ub);
}
}

public void display()


{

for(i=0;i< len;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main()
{
QuickSortInDescendingOrder ob1=new QuickSortInDescendingOrder();
ob1.accept();
System.out.println("Unsorted Array");
ob1.display();

ob1.quickSort(ob1.arr,0,ob1.len-1);
System.out.println("Sorted Array");
ob1.display();

}
}
Question
DISPLAY ELEMENTS WHICH ARE SMITH AND EMIRP FROM
THE ARRAY.
FURTHER SORT THE SMITH AND EMIRP ELEMENTS
DISPLAYED ABOVE INDIVIDUALLY AND DISPLAY THE
COMPLETE SORTED SMITH AND EMIRP ELEMENTS IN
SEPARATE LINE WITH PROPER MESSAGE.
ONLY USING MAIN METHOD.

(AN EMIRP NUMBER (PRIME SPELLED BACKWARDS) IS A PRIME


NUMBER THAT RESULTS IN A DIFFERENT PRIME WHEN ITS
DECIMAL DIGITS ARE REVERSED.)
(A SMITH NUMBER IS A COMPOSITE NUMBER WHOSE SUM OF
DIGITS IS EQUAL TO THE SUM OF DIGITS IN ITS PRIME
FACTORIZATION)
ENTER THE SIZE OF ARRAY:
10
ENTER ELEMENTS IN ARRAY:
101
3
22
4
666
12
31
13
113
311
EMIRP NUMBERS UNSORTED:
101 3 31 13 113 311
SMITH NUMBER UNSORTED:
22 4 666
EMIRP NUMBERS SORTED:
3 13 31 101 113 311
SMITH NUMBER SORTED:
4 22 666
Answer

import java.util.Scanner;
public class array
{

public static void main()


{
int arr[],emirpArray[],SmithArray[],tempArr[];
int n,i=0,count=0,flag=0,flag1=0,rev=0,temp=0,k=0,sumd=0,sump=0,j=0,temp1=0,l=0,m=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of array:");
n=sc.nextInt();
arr=new int[n];
emirpArray=new int[n];
SmithArray=new int[n];
System.out.println("Enter elements in array:");
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
//Emirp Number Checking
for(i=0;i< n;i++)
{
if(arr[i]>1)
{
for(j=2;j< arr[i];j++)
{
if(arr[i]%j==0)
{
flag=1;
break;
}
}
}
else
{
flag=1;
}
temp=arr[i];
while(temp>0)
{
rev=rev*10+temp%10;
temp=temp/10;
}
if(rev>1)
{
for(j=2;j< rev;j++)
{
if(rev%j==0)
{
flag1=1;
break;
}
}
}
else
{
flag1=1;
}
if(flag==0&&flag1==0)
{

emirpArray[k]=arr[i];
k++;
}
//Emirp number checking Ends here
flag=0;
flag1=0;
rev=0;
sump=0;
sumd=0;
//Smith number checking Starts Here
temp=arr[i];
while(temp>0)
{
sumd=sumd+(temp%10);
temp=temp/10;
}
temp=arr[i];
for(j=2;j<=arr[i]/2;j++)
{
while(temp%j==0)
{
flag=0;
//checking whether number is prime number or not
for(m=2;m< j;m++)
{

if(m%j==0)
{
flag=1;
break;
}
}
//if number is prime then only it is added to sump
if(flag==0)
{
temp1=m;
//Extracting digits if i >9 and then adding extracted digits to sump
while(temp1>0)
{
sump=sump+(temp1%10);
temp1=temp1/10;
}
temp=temp/m;

}
}

if(sumd==sump && arr[i]>0)


{
SmithArray[l]=arr[i];
l++;
}
//Smith number checking ends Here

}
//Creating emirpArray
tempArr=new int[k];
for(i=0;i< k;i++)
{
tempArr[i]=emirpArray[i];
}
emirpArray=tempArr;
tempArr=new int[l];
//Creating smithArray
for(i=0;i< l;i++)
{
tempArr[i]=SmithArray[i];
}
SmithArray=tempArr;
System.out.println("Emirp Numbers Unsorted:");
//Printing Emirp Number array
for(i=0;i< k;i++)
{
System.out.print(emirpArray[i]+" ");
}

System.out.println();
System.out.println("Smith Number Unsorted:");
//Printing Smith Number array
for(i=0;i< l;i++)
{
System.out.print(SmithArray[i]+" ");
}

//sorting emirp number array


for(i=0;i< k;i++)
{
for(j=0;j< k;j++)
{
if(emirpArray[i]< emirpArray[j])
{
temp=emirpArray[i];
emirpArray[i]=emirpArray[j];
emirpArray[j]=temp;
}
}
}
System.out.println();
System.out.println("Emirp Numbers Sorted:");

for(i=0;i< k;i++)
{
System.out.print(emirpArray[i]+" ");
}
//sorting smith number array
for(i=0;i< l;i++)
{
for(j=0;j< l;j++)
{
if(SmithArray[i]< SmithArray[j])
{
temp=SmithArray[i];
SmithArray[i]=SmithArray[j];
SmithArray[j]=temp;
}
}
}
System.out.println();
System.out.println("Smith Number sorted:");
for(i=0;i< l;i++)
{
System.out.print(SmithArray[i]+" ");
}
}
}

You might also like