You are on page 1of 50

​Programme Name​:BCS

​Course Code​: CSC- 2604

Course Name​: Programming Fundamental

​ Assignment​:4

Deadline of Submission​:25​th​Aug​, 2020

Submitted By: Submitted To:

Student Name: Nabin Gautam Faculty Name: BCS

IUKL ID: Department: Sunway college

Semester: First (1​st​)


1. Write a java program to input n integer values from user, store it
into an array and print all the elements.

import java.util.Scanner;

public class ArrayPrint{

public static void main(String []args){

Scanner sc= new Scanner(System.in);

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

int num=sc.nextInt();

int[] arr=new int[num];

System.out.printf("Enter %d numbers: ",num);

for(int i=0;i<num;i++){

arr[i]=sc.nextInt();

System.out.println("The number you have entered is: ");

for(int i=0;i<num;i++){

System.out.println(arr[i]);

Output:

Enter the number of elements


5

Enter 5 numbers: 33 5 77 2 10

The number you have entered is:

33

77

10

2. Write a Java program to sum values of an array input from user.


Also calculate the average value of the elements​.

import java.util.Scanner;

public class ArraySumAverage{

public static void main(String []args){

Scanner sc= new Scanner(System.in);

System.out.println("Enter the size of the arrays:");

int num=sc.nextInt();

double sum=0,average=0;

int[] arr=new int[num];

System.out.printf("Enter %d numbers: ",num);

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

sum+=arr[i];

average=sum/num;

System.out.printf("The sum of the numbers are %f and average is


%f",sum,average);

Output:

Enter the size of the arrays:

Enter 5 numbers: 2 3 44 1 6

The sum of the numbers are 56.000000 and average is 11.200000

3. Write a Java program to test if an array contains a specific value.


(Linear Search).

import java.util.Scanner;
public class Array3{
public static void main(String args[]){
int i,num, item, array[];
Scanner input = new Scanner(System.in);
System.out.println("Enter size of the array:");
num = input.nextInt();
array = new int[num];
System.out.println("Enter " + num + " integers");
for ( i = 0; i < num; i++)
array[i] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
for ( i = 0; i < num; i++)
{
if (array[i] == item)
{
System.out.println(item+" is present at location "+(i+1));
break;
}
}
if ( i == num)
System.out.println(item + " doesn't exist in array.");
}
}
Output:
Enter size of the array:
5
Enter 5 integers
33 2 1 6 3
Enter the search value:
6
6 is present at location 4

4. Write a Java program to find the index of an array element​.

import java.util.Scanner;

public class IndexOfArray{

public static void main(String []args){

Scanner sc= new Scanner(System.in);


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

int num=sc.nextInt();

int[] arr=new int[num];

System.out.printf("Enter %d numbers: ",num);

for(int i=0;i<num;i++){

arr[i]=sc.nextInt();

System.out.println("Enter the position of index: ");

int x=sc.nextInt();

System.out.println("In this index position it present "+arr[x]);

Output:

Enter size of the array:

Enter 6 numbers: 2 55 7 22 9 100

Enter the position of index:

In this index position it present 22

5. Write a Java program to copy an array by iterating the array

import java.util.Scanner;
public class CopyingArray{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] firsta=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
firsta[i]=sc.nextInt();
}
int [] seconda=new int[num];
for(int i=0;i<firsta.length;i++){
seconda[i]=firsta[i];
System.out.println("The copy part of first array of index "+i+" is
"+seconda[i]);
}
}
}
Output:
Enter size of the array:
4
Enter 4 numbers: 2 4 7 8
The copy part of first array of index 0 is 2
The copy part of first array of index 1 is 4
The copy part of first array of index 2 is 7
The copy part of first array of index 3 is 8

6. Write a Java program to find the maximum and minimum value


of an array.
public class MaxMinArray{

public static void main(String []args){

int[] arr=new int[]{5,66,8,9,3};

int max=arr[0];

int min=arr[0];

for(int i=1;i<arr.length;i++){

if(arr[i]>max){

max=arr[i];

else if(arr[i]<min){

min=arr[i];

System.out.println("The maximum value is "+max);

System.out.println("The minimum value is "+min);

}
Output:

The maximum value is 66


The minimum value is 3

7. Write a Java program to get the difference between the largest


and smallest values in an array of integers. The length of the
array must be 1 and above.

public class DiffOfMaxMin{

public static void main(String []args){

int[] arr=new int[]{5,66,8,9,3};

int max=arr[0];

int min=arr[0];

for(int i=1;i<arr.length;i++){

if(arr[i]>max){

max=arr[i];

else if(arr[i]<min){

min=arr[i];

System.out.println("The difference between maximum and


minimum value is "+(max-min));

}
Output:
The difference between maximum and minimum value is 63

8. Write a Java program to sort an integer array in ascending order


using bubble sort.

import java.util.Scanner;

public class AscendingArray{

public static void main(String [] args){

Scanner sc= new Scanner(System.in);

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

int num=sc.nextInt();

int [] arr=new int[num];

System.out.printf("Enter %d numbers:",num);

for(int i=0;i<num;i++){

arr[i]=sc.nextInt();

System.out.println("Array before sorting");

int size=arr.length;

for(int n:arr){

System.out.print(n+" ");

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


for(int j=0;j<size-1-i;j++){

if(arr[j]>arr[j+1]){

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

System.out.println("\nArray after Sorting "+(i+1));

for(int n:arr){

System.out.print(n+" ");

}
Output:

Enter size of array:

Enter 5 numbers:1 55 3 88 4

Array before sorting

1 55 3 88 4
Array after Sorting 1

1 3 55 4 88

Array after Sorting 2

1 3 4 55 88

Array after Sorting 3

1 3 4 55 88

Array after Sorting 4

1 3 4 55 88

Array after Sorting 5

1 3 4 55 88

9. Write a Java program to find the second largest element in an


array.

public class SecondLargest{

public static void main(String [] args){

int arr[]={2,66,4,8,555};

int largest=arr[0];

int secondLargest=arr[0];

for(int i=0;i<arr.length;i++){

if(arr[i]>largest){
secondLargest=largest;

largest=arr[i];

else if(arr[i]>secondLargest){

secondLargest=arr[i];

System.out.println("The second highest value is:


"+secondLargest);

}
Output:
The second highest value is: 66

10.Write a Java program to reverse an array of integer values​.

import java.util.Scanner;

import java.util.Arrays;

public class ReverseArray{

public static void main(String [] args){

Scanner sc= new Scanner(System.in);

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

int num=sc.nextInt();
int [] arr=new int[num];

System.out.printf("Enter %d numbers:",num);

for(int i=0;i<num;i++){

arr[i]=sc.nextInt();

int size=arr.length;

for(int i=0;i<size/2;i++){

int a=arr[i];

arr[i]=arr[arr.length-i-1];

arr[arr.length-i-1]=a;

System.out.println("Reverse value:"+Arrays.toString(arr));

Output:

Enter size of array:

Enter 7 numbers:1 2 3 4 5 6 7

Reverse value:[7, 6, 5, 4, 3, 2, 1]
11. Write a Java program to find the duplicate values of an array
of integer values.

import java.util.Scanner;
public class DuplicateArray{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of array:");
int num=sc.nextInt();
int [] arr=new int[num];
System.out.printf("Enter %d numbers:",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j]){
System.out.println("Duplicate elements is: "+arr[i]);
}
}
}
}
}
Output:
Enter size of array:
10
Enter 10 numbers:2 5 7 3 6 2 4 3 1 3
Duplicate elements is: 2
Duplicate elements is: 3
Duplicate elements is: 3
Duplicate elements is: 3
12. Write a Java program to find the common elements between
two arrays of integers.
public class CommonArray{
public static void main(String [] args){
int []a={1,2,3,4,5};
int []b={1,3,5,7,9};
for (int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if(a[i]==b[j]){
System.out.println(a[i]+" ");
break;
}
}
}
}
}
Output:
1
3
5

13. Write a Java program to add two matrices of the same size.

import java.util.Scanner;

public class AddMatrix{

public static void main(String [] args){

Scanner sc= new Scanner(System.in);

System.out.println("Enter size of matrix:");

int row=sc.nextInt();
int column=sc.nextInt();

int [] [] a=new int[row][column];

int [] [] b=new int[row][column];

int [] [] c=new int[row][column];

System.out.printf("Enter element:");

for(int i=0;i<row;i++){

for(int j=0;j<column;j++){

System.out.printf("array [%d][%d]:",i, j);

a[i][j]=sc.nextInt();

System.out.println("Enter another matrix:");

for(int i=0;i<row;i++){

for(int j=0;j<column;j++){

System.out.printf("array [%d][%d]:",i, j);

b[i][j]=sc.nextInt();

for(int i=0;i<row;i++){
for(int j=0;j<column;j++){

c[i][j]=a[i][j]+b[i][j];

System.out.println("The sum of two matrix is: "+c[i][j]);

}
Output:

Enter size of matrix:

33

Enter element:array [0][0]:1

array [0][1]:2

array [0][2]:3

array [1][0]:4

array [1][1]:5

array [1][2]:6

array [2][0]:7

array [2][1]:8

array [2][2]:9

Enter another matrix:

array [0][0]:9
array [0][1]:8

array [0][2]:7

array [1][0]:6

array [1][1]:5

array [1][2]:4

array [2][0]:3

array [2][1]:2

array [2][2]:1

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

The sum of two matrix is: 10

14. Write a java program to take a 2d array input from user and
print the sum of each row, column and total sum in as shown
below:
import java.util.Scanner;
public class SumOfRowColumn{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of matrix:");
int m=sc.nextInt();
int n=sc.nextInt();
int [] [] arr=new int[m][n];
System.out.printf("Enter the number");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
System.out.printf("array [%d][%d]:",i, j);
arr[i][j]=sc.nextInt();
}
}
int sumRow=0;
for(int i=0;i<m;i++){
sumRow=0;
for(int j=0;j<n;j++){
sumRow+=arr[i][j];
System.out.print(arr[i][j]+" ");
}
System.out.println(sumRow+" ");
}
int sumCol=0;
int sum=0;
for(int j=0;j<n;j++){
sumCol=0;
for(int i=0;i<m;i++){
sumCol+=arr[i][j];

}
sum+=sumCol;
System.out.print(sumCol+" ");
}
System.out.println(sum);
}
}
Output:
Enter size of matrix:
33
Enter the numberarray [0][0]:1
array [0][1]:2
array [0][2]:3
array [1][0]:4
array [1][1]:5
array [1][2]:6
array [2][0]:7
array [2][1]:8
array [2][2]:9
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45

15. Write a Java program to input two matrices order, check if the
order is eligible to multiply, if yes input the two matrices,
multiply them and display the result.
import java.util.Scanner;

public class MatrixMultiplication{


public static void main(String args[]){
int m, n, p, q, sum = 0,i,j,k;

Scanner in = new Scanner(System.in);


System.out.println("Enter the number of rows and columns of first
matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];

System.out.println("Enter elements of first matrix");

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


for (j = 0; j< n; j++)
first[i][j] = in.nextInt();

System.out.println("Enter the number of rows and columns of


second matrix");
p = in.nextInt();
q = in.nextInt();

if (n != p)
System.out.println("The matrices can't be multiplied with each
other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

System.out.println("Enter elements of second matrix");

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


for (j = 0; j< q; j++)
second[i][j] = in.nextInt();

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


for (j = 0; j< q; j++) {
for (k = 0; k < p; k++)
sum = sum + first[i][k]*second[k][j];

multiply[i][j] = sum;
sum = 0;
}
}

System.out.println("Product of the matrices:");

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


for (j = 0; j < q; j++)
System.out.print(multiply[i][j]+"\t");

System.out.print("\n");
}
}
}
}
Output:
Enter the number of rows and columns of first matrix
3
3
Enter elements of first matrix
1
2
1
3
4
6
8
9
5
Enter the number of rows and columns of second matrix
3
3
Enter elements of second matrix
2
5
8
2
9
0
3
2
5
Product of the matrices:
9 25 13
32 63 54
49 131 89

16. Write a Java program to input a matrix A and create its


transpose matrix A​T​ and print the result​.

import java.util.Scanner;

public class TransposeAMatrix{

public static void main(String [] args){

Scanner sc= new Scanner(System.in);

System.out.println("Enter size of matrix:");

int m=sc.nextInt();
int n=sc.nextInt();

int [] [] arr=new int[m][n];

System.out.printf("Enter the number");

for(int i=0;i<m;i++){

for(int j=0;j<n;j++){

System.out.printf("array [%d][%d]:",i, j);

arr[i][j]=sc.nextInt();

int transpose[][]=new int [n][m];

for (int i=0;i<m;i++){

for(int j=0;j<n;j++){

transpose[j][i]=arr[i][j];

System.out.println("Transpose of the given matrix is: ");

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

for(int j=0;j<m;j++){

System.out.print(transpose[i][j]+" ");

System.out.println();
}

Output:

Enter size of matrix:

33

Enter the numberarray [0][0]:1

array [0][1]:2

array [0][2]:1

array [1][0]:6

array [1][1]:8

array [1][2]:4

array [2][0]:6

array [2][1]:2

array [2][2]:9

Transpose of the given matrix is:

166

282

149
17. Write a Java program to find all pairs of elements in an array
whose sum is equal to a specified number.
import java.util.Scanner;
public class PairOfSum{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int a=0;a<num;a++){
arr[a]=sc.nextInt();

for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]+arr[j]==arr[a]){
System.out.println(arr[i]+"+"+arr[j]+ " ="+arr[a]);
}
}
}
}
}
}
Output:
Enter size of array:
5
Enter 5 numbers: 1 2 3 4 5
1+0 =1
1+0 =1
1+0 =1
1+0 =1
2+0 =2
2+0 =2
2+0 =2
1+2 =3
3+0 =3
3+0 =3
1+3 =4
4+0 =4
1+4 =5
2+3 =5

18. Write a Java program to test the equality of two arrays.


import java.util.Scanner;
public class EqualityTest{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the first array:");
int num=sc.nextInt();
int[] arrayOne=new int[num];
int[] arrayTwo=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arrayOne[i]=sc.nextInt();
}
System.out.println("Enter size of the second array:");
int num1=sc.nextInt();
System.out.printf("Enter %d numbers: ",num1);
for(int i=0;i<num1;i++){
arrayTwo[i]=sc.nextInt();
}
boolean equalOrNot=false;
if(arrayOne.length==arrayTwo.length){
for(int i=0;i<arrayOne.length;i++){
if(arrayOne[i]!=arrayTwo[i]){
equalOrNot=true;
}
}
}
else{
equalOrNot=false;
}
if(equalOrNot){
System.out.println("two array are equal");
}
else{
System.out.println("two array arenot equal");
}
}
}
Output:
Enter size of the first array:
5
Enter 5 numbers: 1
23
4
5
6
Enter size of the second array:
5
Enter 5 numbers: 1
8
9
5
6
two array are equal
19. Write a Java program to find the number of even and odd
integers in a given array of integers.
import java.util.Scanner;
public class EvenOddArray{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();

int count=0;
if(arr[i]%2==0){
System.out.println(arr[i]+":Even Number");
}
else {
System.out.println(arr[i]+":Odd Number");
}
}
}
}
Output:
Enter size of the array:
5
Enter 5 numbers: 1
1:Odd Number
23
23:Odd Number
4
4:Even Number
56
56:Even Number
7
7:Odd Number

20. Write a Java program to compute the average value of an


array of integers except the largest and smallest values.
public class AverageWithoutMaxMin{
public static void main(String[] args)
{
int[] arr = {9, 6, 26, 48, 98};
int max = arr[0];
int min = arr[0];
float sum = arr[0];
for(int i = 1; i < arr.length; i++)
{
sum += arr[i];
if(arr[i] > max)
max = arr[i];
else if(arr[i] < min)
min = arr[i];
}
System.out.printf("The maximum value is %d and minimum
value is %d.",max,min);
System.out.println();
float x = ((sum-max-min) / (arr.length - 2));
System.out.printf("The average value of an array of integers
except the largest and smallest values: %.2f",x);
System.out.print("\n");
}
}
Output:
The maximum value is 98 and minimum value is 6.
The average value of an array of integers except the largest and
smallest values: 27.67

21. Write a Java program to check if an array of integers without


0 and -1.
import java.util.Scanner;
public class Test0And1{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] arr=new int[num];
boolean checkZero=false,checkOne=false;
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
for(int i=0;i<num;i++){
if(arr[i]==0){
checkZero=true;
}
else if(arr[i]==-1){
checkOne=true;
}
}
if(checkZero==true &&checkOne==true){
System.out.println("array contain 0 & -1.");
}
else{
System.out.println("array dosen't contain 0 & -1.");
}
}
}
Output:
Enter thte size of array:
5
Enter 5 numbers: 0 2 3 4 -1
array contain 0 & -1.

22. Write a Java program to check if an array of integers contains


two specified elements 65 and 77​.

import java.util.Scanner;

public class TwoSpecifiedNum{

public static void main(String []args){

Scanner sc= new Scanner(System.in);

System.out.println("Enter size of the arrays:");

int num=sc.nextInt();

int[] arr=new int[num];

boolean checkOne=false,checkTwo=false;

System.out.printf("Enter %d numbers: ",num);

for(int i=0;i<num;i++){

arr[i]=sc.nextInt();

for(int i=0;i<num;i++){

if(arr[i]==65){

checkOne=true;

}
else if(arr[i]==77){

checkTwo=true;

if(checkOne==true &&checkTwo==true){

System.out.println("array contain 65 & 77.");

else{

System.out.println("array dosen't contain 65 & 77.");

}
Output:
Enter size of the arrays:
6
Enter 6 numbers: 22
44
66
77
87
65
array contain 65 & 77.

23. Write a Java program to remove the duplicate elements of a


given array and return the new length of the array.
Sample array: [20, 20, 30, 40, 50, 50, 50]
After removing the duplicate elements the program should
return 4 as the new length of the array.
import java.util.Scanner;
public class RemoveDuplicate{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the size of array:");
int num=sc.nextInt();
int[] arr=new int[num];
int[] newArr=new int[arr.length];
System.out.printf("Enter %d numbers: ",num);
for(int a=0;a<num;a++){
arr[a]=sc.nextInt();
}
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
int no_unique_elements=arr.length;
int size=0;
for(int i=0;i<arr.length;i++){
boolean copy=true;
for(int j=0;j<size;j++){
if(arr[i]==newArr[j]){
copy=false;
break;
}
}
if(copy==true){
newArr[size]=arr[i];
size++;
}
}
int[]array1=new int[size];
for(int i=0;i<size;i++){
array1[i]=newArr[i];
}
System.out.println();
System.out.println("Array with unique values: ");
for(int i=0;i<array1.length;i++){
System.out.print(array1[i]+" ");
}
System.out.println();
}
}
Output:
Enter the size of array:
6
Enter 6 numbers: 1
3
66
1
8
5
1 3 66 1 8 5
Array with unique values:
1 3 66 8 5

24. Write a Java program to find the sum of the two elements of a
given array which is equal to a given integer.
Sample array: [1,2,4,5,6]
Target value: 6.

import java.util.Scanner;
public class TargetedSum{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
System.out.println("Enter the target sum value:");
int x=sc.nextInt();
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]+arr[j]==x){
System.out.println(arr[i]+"+"+arr[j]+ " ="+x);
}
}
}
}
}
Output:
Enter size of array:
5
Enter 5 numbers: 12
44
7
2
9
Enter the target sum value:
9
7+2 =9
25. Write a Java program to print all the LEADERS in the array.
Note: An element is leader if it is greater than all the elements to
its right side.
import java.util.Scanner;
public class Leaders{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int a=0;a<num;a++){
arr[a]=sc.nextInt();
}
int size=arr.length;
for (int i = 0; i < size; i++)
{
int j;
for (j = i + 1; j < size; j++)
{
if (arr[i] <= arr[j])
break;
}
if (j == size)
System.out.print(arr[i] + " " +"is the leaders.");
}
}
}
Output:
Enter size of the array:
5
Enter 5 numbers: 1 55 22 9 6
55 is the leaders.22 is the leaders.9 is the leaders.6 is the leaders.
26. Write a Java program to find smallest and second smallest
elements of a given array.

public class SmallAndSecondSmall{


public static void main(String [] args){
int arr[]={22,66,4,8,555};
int small=arr[0];
int secondSmall=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]<small){
secondSmall=small;
small=arr[i];
}
else if(arr[i]<secondSmall){
secondSmall=arr[i];
}
}
System.out.printf("The lowest is %d and second lowest value is
%d. ",small,secondSmall);
}
}
Output:
The lowest is 4 and second lowest value is 8.

27. Write a Java program to segregate all 0s on left side and all 1s
on right side of a given array of 0s and 1s.

public class Seperate0And1{


public static void main(String [] args){
int[] arr={0,1,0,1,1,0,1};
int j=0;
for(int i=0;i<arr.length;i++){
if(arr[i] == 0) {
arr[j++] = arr[i];
}
}
while(j < arr.length) {
arr[j++] = 1;
}
for(int k = 0; k < arr.length; k++) {
System.out.print(arr[k] + " ");
}
}
}

Output:
0001111

28.Write a Java program to cyclically rotate a given array clockwise


by one​.
public class CyclicOrder{
public static void main(String [] args){
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int n = arr.length;
int last, i=0;
System.out.print("The original array is: ");
for (i = 0; i < n; ++i){
System.out.print(arr[i] + " ");
}
last = arr[n-1];
for (i = n-1; i > 0; i--){
arr[i] = arr[i-1];

}
System.out.print("\nThe rotated Array is: ");
arr[0] = last;
for (i = 0; i < n; ++i){
System.out.print(arr[i]+" ");
}
}
}

Output:
The original array is: 1 2 3 4 5 6 7 8
The rotated Array is: 8 1 2 3 4 5 6 7

29. Write a Java program to sort an array of positive integers of a


given array, in the sorted array the value of the first element
should be maximum, second value should be minimum value,
third should be second maximum, fourth second be second
minimum and so on.

import java.util.Scanner;
import java.util.Arrays;
public class SampleMaxi{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] firstArr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
firstArr[i]=sc.nextInt();
}
int size=firstArr.length;
Arrays.sort(firstArr);
System.out.println("sorted Array ::"+Arrays.toString(firstArr));
int [] secondArr=new int[num];
for(int i=0;i<size/2;i++){
secondArr[i]=firstArr[i];
System.out.println(" \nFirst copying part:"+secondArr[i]);
}
int [] thirdArr=new int[num];
for(int i=num-1;i>=size/2;i--){
thirdArr[i]=firstArr[i];
System.out.println("Second copying part:"+thirdArr[i]);
}
int [] result=new int[num];
int j=num-1,k=0;
for( int i=0;i<result.length;i++){
if(i%2==0){
result[i]=thirdArr[j--];
}
else{
result[i]=secondArr[k++];
}
}
for(int i=0;i<num;i++){
System.out.println(result[i]);
}
}
}

Output:
Enter size of the array:
5
Enter 5 numbers: 33 2 66 11 90
sorted Array ::[2, 11, 33, 66, 90]

First copying part:2

First copying part:11


Second copying part:90
Second copying part:66
Second copying part:33
90
2
66
11
33

30. Write a Java program to separate even and odd numbers of a


given array of integers. Put all even numbers first, and then odd
numbers.

public class EvenOddSeperation{


public static void main(String [] args){
int[] arr={50,7,6,8,4,31,1};
int j=0;
for(int i=0;i<arr.length;i++){
if(arr[i] %2== 0) {
arr[j++] = arr[i];
}
}
for(int i=0;i<arr.length;i++){
if(arr[i] %2!= 0) {
arr[j++] = arr[i];
}
}
for(int k = 0; k < arr.length; k++) {
System.out.print(arr[k] + " ");
}
}
}
Output:
50 6 8 4 31 1 7
31. Write a Java program to sort a given binary array in linear
times.

import java.util.Arrays;
import java.util.Scanner;
public class Sorting0And1{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Size of array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d number only 0 and 1 :",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
int k = 0;
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == 0) {
arr[k++] = 0;
}
}
for (int i = k; i < arr.length; i++) {
arr[k++] = 1;
}
System.out.println("arrays after sorting is"+Arrays.toString(arr));
}
}
Output:
Size of array:
5
Enter 5 number only 0 and 1 :0 1 0 1 0
arrays after sorting is [0, 0, 0, 1, 1]
32. Given two sorted arrays A and B of size p and q, write a Java
program to merge elements of A with B by maintaining the
sorted order i.e. fill A with first p smallest elements and fill B
with remaining elements.

33. Write a Java program to shuffle a given array of integers.

import java.util.Scanner;

import java.util.Random;

public class ShuffleArray{

public static void main(String []args){

Scanner sc= new Scanner(System.in);

System.out.println("Enter the size of First Array:");

int num=sc.nextInt();

int[] firstArr=new int[num];

System.out.printf("Enter %d numbers: ",num);

for(int i=0;i<num;i++){

firstArr[i]=sc.nextInt();

int size=firstArr.length;
Random rand=new Random();

int random=rand.nextInt(size);

for(int i=0;i<num;i++){

int temp=firstArr[i];

firstArr[i]=firstArr[random];

firstArr[random]=temp;

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

System.out.println(firstArr[i]+" ");

}
Output:
Enter the size of First Array:
5
Enter 5 numbers: 33 4 2 6 1
4
1
33
2
6

34. Write a Java program to replace each element of the array


with product of every other element in a given array of integers.

import java.util.Scanner;
public class MultiplyAllElements{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the size of First Array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
int size=arr.length;
int product=1;
int [] result=new int[size];
for(int i=0;i<size;i++){
product*=arr[i];

}
for(int i=0;i<size;i++){
result[i]=product/arr[i];

}
System.out.printf("Array with product of every other element:”);
Output:

Enter the size of First Array:


5
Enter 5 numbers: 1
7
3
9
2
Array with product of every other element:
378
54
126
42
189

35. Write a Java program to find maximum difference between


two elements in a given array of integers such that smaller
element appears before larger element.
import java.util.Scanner;
import java.util.Arrays;
public class LargeDifference{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the size of First Array:");
int num=sc.nextInt();
int[] firstArr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
firstArr[i]=sc.nextInt();
}
int size=firstArr.length;
Arrays.sort(firstArr);
System.out.println("sorted Array ::"+Arrays.toString(firstArr));
int max=firstArr[0],min=firstArr[0];
for(int i=1;i<size;i++){
if(firstArr[i]>max){
max=firstArr[i];
}
else if(firstArr[i]<min){
min=firstArr[i];
}
}
System.out.println("The maximum difference between two
elements of the said array elements”);
OutPut:
Enter the size of First Array:
5
Enter 5 numbers: 11
6
33
8
99
sorted Array ::[6, 8, 11, 33, 99]
The maximum difference between two elements of the said array
elements 93

You might also like