Array Program Book Final
Array Program Book Final
Array
Question
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
Question
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);
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
Question
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
Question
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
/*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
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();
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);
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();
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();
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);
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);
for(i=0;i< size;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
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
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);
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:" );
}
}
Question
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();
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);
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] + " ");
}
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
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
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);
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
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);
/* 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
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
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();
/* 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
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);
}
}
Question
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("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
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("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
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;
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
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
Question
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];
}
}
Question
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);
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();
}
}
Question
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
Question
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
}
Question
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);
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
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);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
{
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.
import java.util.Scanner;
public class ReverseNumbersInArray
{
int n=0,i=0,temp=0,reversedInteger=0,remainder=0;
Scanner sc = new Scanner(System.in);
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
}
}
Question
WRITE A PROGRAM TO DETERMINE WHETHER A GIVEN NUMBER IS A
HARSHAD NUMBER.
import java.util.Scanner;
public class HarshadNumberInArray
{
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();
}
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;
}
if(num>1)
{
for (int j = 2; j <= num / 2; j++)
{
if ((num % j) == 0)
{
flag = false;
break;
}
}
}
else
{
flag=false;
}
return flag;
}
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
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);
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.
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);
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();
}
sizeOfArray=sizeOfArray-1;
}
}
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);
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();
INPUT:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
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);
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;
}
}
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
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);
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
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);
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
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);
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;
}
}
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.
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);
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;
}
}
for(i=0;i< n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question
SORT AN ARRAY USING INSERTION SORT IN ASCENDING
ORDER.
import java.util.Scanner;
public class InsertionSortAscendingOrder
{
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.
import java.util.Scanner;
public class InsertionSortDescendingOrder
{
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.
import java.util.Scanner;
public class QuickSortInAscendingOrder
{
int arr[];
int len=0;
int i=0;
public void accept()
{
for(i=0;i< len;i++)
{
System.out.print("Enter the Number:");
arr[i]=sc.nextInt();
}
}
}
}
return end;
}
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.
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();
}
}
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.
import java.util.Scanner;
public class array
{
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;
}
}
}
//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]+" ");
}
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]+" ");
}
}
}