> ARRAY <
Edited and compiled by Souradeep Roy using Intelij IDEA (community version)
1.1 Write a program in Java to prove the execution and concept of array.
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Gup
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
int i;
int [] arr={0,1,2,3,4,5};
System.out.println("The Array is :");
for(i=0;i<=5;i++)
{
System.out.println(arr[i]);
}
}
}
Output:-
--------
The Array is :
0
1
2
3
4
5
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.2 Write a Program in Java to prove the execution of array by taking input
from the user.
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int i,n;
System.out.println("Enter the number of elements");
n=input.nextInt();
int[] arr= new int[100];
System.out.println("Enter the elements of the array");
for(i=0;i<n;i++)
{
arr[i]=input.nextInt();
}
System.out.println("Array elements are ");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
}
Output:-
--------
Enter the number of elements
3
Enter the elements of the array
56
45
12
Array elements are
56
45
12
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.3 Write a program with while loop to control array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int i=0,n;
System.out.println("Enter the number of elements");
n=input.nextInt();
int[] arr= new int[100];
System.out.println("Enter the elements of the array");
while(i<n)
{
arr[i]=input.nextInt();
i++;
}
System.out.println("Array elements are ");
i=0;
while(i<n)
{
System.out.println(arr[i]);
i++;
}
}
}
Output:-
--------
Enter the number of elements
3
Enter the elements of the array
56
45
12
Array elements are
56
45
12
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.4 Write a program to show reverse of the elements of an array
________________________________________________________________________________________
_____________________________________
CODE:-
------
//reverse an array//
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int i,n;
System.out.println("Enter the number of elements ");
n=input.nextInt();
int [] arr=new int[100];
System.out.println("Enter the elements of the array ");
for(i=0;i<n;i++)
{
arr[i]=input.nextInt();
}
System.out.println("Entered elements in reverse order ");
for(i=n-1;i>=0;i--)
{
System.out.println(arr[i]);
}
}
}
Output:-
--------
Enter the number of elements
3
Enter the elements of the array
56
96
45
Entered elements in reverse order
45
96
56
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.5 Write a program to add the elements of an array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int i,sum=0,n;
System.out.println("Enter the number of elements ");
n=input.nextInt();
int [] arr=new int[100];
System.out.println("Enter the elements in the array ");
for(i=0;i<n;i++)
{
arr[i]=input.nextInt();
}
System.out.println("Entered elements are :");
for(i=0;i<n;i++)
{
sum+=arr[i];
System.out.println(arr[i]);
}
System.out.println("The sum of the entered elements is ="+sum);
}
}
Output:-
--------
Enter the number of elements
5
Enter the elements in the array
23
45
56
12
45
Entered elements are :
23
45
56
12
45
The sum of the entered elements is =181
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.6 Write a program to add the runs of ten batsmen with average
using array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int i;
float sum=0,avg=0;
int[] arr=new int[100];
System.out.println("Enter the runs ");
for(i=1;i<=11;i++)
{
arr[i]=input.nextInt();
}
System.out.println("Entered runs are");
for(i=1;i<=11;i++)
{
sum+=arr[i];
System.out.println(arr[i]);
}
avg=sum/11;
System.out.printf("Total Runs =%.0f\nAverage = %.2f",sum,avg);
}
}
Output:-
--------
Enter the runs
50
45
65
12
36
65
46
78
25
41
3
Entered runs are
50
45
65
12
36
65
46
78
25
41
3
Total Runs =466
Average = 42.36
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.7 WAP in Java to count positive, negative, odd & even numbers in
an array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int Size, i;
int positiveCount = 0, negativeCount = 0;
int evenCount = 0, oddCount = 0;
sc = new Scanner(System.in);
System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();
int [] a = new int[Size];
System.out.print(" Please Enter " + Size + " elements of an Array : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}
for(i = 0; i < Size; i++)
{
if(a[i] >= 0&& a[i]%2==0)
{
positiveCount++;
evenCount++;
}
else
{
negativeCount++;
oddCount++;
}
}
System.out.println("\n Total Number of Positive Numbers in this Array = " +
positiveCount);
System.out.println("\n Total Number of Negative Numbers in this Array = " +
negativeCount);
System.out.println("\n Total Number of Even Numbers in this Array = " +
evenCount);
System.out.println("\n Total Number of Odd Numbers in this Array = " +
oddCount);
}
}
Output:-
--------
Please Enter Number of elements in an array : 5
Please Enter 5 elements of an Array :
-1
9
2
6
-3
Total Number of Positive Numbers in this Array = 2
Total Number of Negative Numbers in this Array = 3
Total Number of Even Numbers in this Array = 2
Total Number of Odd Numbers in this Array = 3
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.8 /*Program to make a selection sort of an array*/
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [] a=new int[25];
int i,j,t;
System.out.println("Enter the Elements of the array\n");
for(i=0;i<=10;i++)
{
System.out.printf("%d\t",i);
a[i]= sc.nextInt();
}
for(i=0;i<=10;i++)
{
for(j=i+1;j<=10;j++)
{
if(a[i]>a[j]) //In this approach, we will
choose a min_index and assign it a value
of the first location of the array. However, in the next iterations,
its value keeps on increasing by 1.
The significance of this
variable is to swap the smaller digits in
the front of the array so that our array automatically gets sorted.
However, we will then choose the smallest digit from the array
and swap it
with a[min_index].
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.printf("\n Sorted Numbers are \n");
for(i=0;i<=10;i++)
{
System.out.printf("%d ",a[i]);
}
System.out.printf("\n\n Press Any Key to Exit");
}
}
Output:-
--------
Enter the Elements of the array
0 1
1 2
2 3
3 8
4 9
5 4
6 5
7 6
8 3
9 1
10 2
Sorted Numbers are
1 1 2 2 3 3 4 5 6 8 9
Press Any Key to Exit
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.9 /*Programs to make an exchange sort of an array*/
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [] a=new int[25];
int i,j,m,t;
System.out.printf("Enter the Elements of the array\n");
for(i=0;i<=10;i++)
{
System.out.printf("%d\t",i);
a[i]= sc.nextInt();
}
for(i=0;i<=10;i++)
{
for(j=0;j<10-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.printf("\n Sorted Numbers are \n");
for(i=0;i<=10;i++)
{
System.out.printf("%d ",a[i]);
}
System.out.printf("\n\n Press Any Key to Exit");
}
}
Output:-
--------
Enter the Elements of the array
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 9
10 6
Sorted Numbers are
1 2 3 4 5 6 6 7 8 9 9
Press Any Key to Exit
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.10 //Write a program to add two arrays
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [] a=new int[25];
int [] b=new int[25];
int [] c=new int[25];
int i;
System.out.printf("\n 1st Array..............\n\n");
for(i=0;i<5;i++)
{
System.out.printf("Enter the Element %d ",i);
a[i]= sc.nextInt();
}
System.out.printf("\n 2nd Array..............\n\n");
for(i=0;i<5;i++)
{
System.out.printf("Enter the Element %d ",i);
b[i]= sc.nextInt();
}
System.out.printf("\n Result Array..............\n\n");
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
System.out.printf(" %d + %d = %d\n",a[i],b[i],c[i]);
}
System.out.printf("Press Any Key to Exit");
}
}
Output:-
--------
1st Array..............
Enter the Element 0 1
Enter the Element 1 2
Enter the Element 2 3
Enter the Element 3 4
Enter the Element 4 5
2nd Array..............
Enter the Element 0 4
Enter the Element 1 5
Enter the Element 2 6
Enter the Element 3 3
Enter the Element 4 2
Result Array..............
1 + 4 = 5
2 + 5 = 7
3 + 6 = 9
4 + 3 = 7
5 + 2 = 7
Press Any Key to Exit
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.11 Java program to find the largest element in an array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size, i, big;
System.out.printf("\nEnter the size of the array: ");
size= sc.nextInt();
int [] a=new int[size];
System.out.printf("\nEnter %d elements in to the array:", size);
for (i = 0; i < size; i++)
a[i]= sc.nextInt();
big = a[0];
for (i = 1; i < size; i++)
{
if (big < a[i])
big = a[i];
}
System.out.printf("\nBiggest: %d ", big);
System.out.printf("\nPress any key to exit");
}
}
Output:-
--------
Enter the size of the array: 5
Enter 5 elements in to the array:
1
6
9
4
7
Biggest: 9
Press any key to exit
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.12 JAVA program to find the second largest element in an
array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int [] a=new int[100];
int size,i,j=0,big,secondbig;
System.out.printf("Enter the size of the array: ");
size= sc.nextInt();
System.out.printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
a[i]= sc.nextInt();
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
{
big=a[i];
j = i;
}
}
secondbig=a[size-j-1];
for(i=1;i<size;i++)
{
if(secondbig <a[i] && j != i)
secondbig =a[i];
}
System.out.printf("Second biggest: %d", secondbig);
System.out.printf("\n\nPress any key to exit.............");
}
}
Output:-
--------
Enter the size of the array: 5
Enter 5 elements in to the array:
1
5
6
7
4
Second biggest: 6
Press any key to exit.............
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.13 JAVA program to find the second smallest element in an
array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int [] a=new int[100];
int size,i,j=0,small,secondsmall;
System.out.printf("Enter the size of the array: ");
size= sc.nextInt();
System.out.printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
a[i]= sc.nextInt();
small=a[0];
for(i=1;i<size;i++)
{
if(small>a[i])
{
small=a[i];
j = i;
}
}
secondsmall=a[size-j-1];
for(i=1;i<size;i++)
{
if(secondsmall >a[i] && j != i)
secondsmall =a[i];
}
System.out.printf("Second smallest: %d", secondsmall);
}
}
Output:-
--------
Enter the size of the array: 5
Enter 5 elements in to the array:
1
2
3
4
5
Second smallest: 2
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.14 JAVA code to find largest and smallest number in an
array
________________________________________________________________________________________
_____________________________________
CODE:-
------
import java.util.Scanner;
public class POP
{
public static void main(String[] args )
{
Scanner input=new Scanner(System.in);
int i,n;
System.out.println("Enter the size of the arrray ");
n= input.nextInt();
int [] arr=new int[n];
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
System.out.println("Enter the elements of the array ");
for(i=0;i<n;i++)
{
arr[i]= input.nextInt();
}
for(i=0;i<n;i++)
{
if(arr[i]<min)
min=arr[i];
if(arr[i]>max)
max=arr[i];
}
System.out.println("Max element is "+max);
System.out.println("Min element is "+min);
}
}
Output:-
-------
Enter the size of the arrray
5
Enter the elements of the array
1
2
5
6
7
Max element is 7
Min element is 1
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.15 Write a program to get the frequency of the number
________________________________________________________________________________________
__________________________________
CODE:-
------
import java.util.Scanner;
public class POP
{
public static void main(String[] args )
{
Scanner input=new Scanner(System.in);
int [] num=new int[20];
int i,count=0,n;
System.out.printf("Enter 10 Elements of array");
for(i=0;i<=9;i++)
{
System.out.printf("\n%d => ",i);
num[i]=input.nextInt();
}
System.out.printf("Enter the Element to search ");
n= input.nextInt();
for(i=0;i<=9;i++)
{
if (num[i]==n)
count++;
}
System.out.printf("\n The Number %d found %d time(s) in the Array",n,count);
}
}
Output:-
-------
Enter 10 Elements of array
0 => 1
1 => 5
2 => 6
3 => 3
4 => 4
5 => 5
6 => 7
7 => 8
8 => 9
9 => 4
Enter the Element to search 5
The Number 5 found 2 time(s) in the Array
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
Double Dimension Array
_______________________
1.16 Write A Program To Create A Double Dimension Array
________________________________________________________________________________________
__________________________________
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] a = {{1, 2, 3}, {4, 5, 6},{7,8,9}};
for (int i = 0; i < a.length; ++i)
{
for (int j = 0; j < a[i].length; ++j)
{
System.out.printf("Elements in [%d,%d] = %d\n",i,j,a[i][j]);
}
}
}
}
Output:-
-------
Elements in [0,0] = 1
Elements in [0,1] = 2
Elements in [0,2] = 3
Elements in [1,0] = 4
Elements in [1,1] = 5
Elements in [1,2] = 6
Elements in [2,0] = 7
Elements in [2,1] = 8
Elements in [2,2] = 9
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.17 Write A Program To Accept And Show A Double Dimension
Array
________________________________________________________________________________________
__________________________________
CODE:-
------
import java.util.Scanner;
public class POP
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter 2D array size : ");
int rows=sc.nextInt();
int columns=sc.nextInt();
System.out.println("Enter array elements : ");
int twoD[][]=new int[rows][columns];
for(int i=0; i<rows;i++)
{
for(int j=0; j<columns;j++)
{
twoD[i][j]=sc.nextInt();
}
}
System.out.print("\nData you entered : \n");
for(int []x:twoD)
{
for(int y:x){
System.out.print(y+" ");
}
System.out.println();
}
}
}
Output:-
-------
Enter 2D array size : 3
3
Enter array elements :
1
2
3
4
5
6
7
8
9
Data you entered :
1 2 3
4 5 6
7 8 9
----------------------------------------------------------------------------------------
-------------------------------------
1.18 Write a program to accept and show and sum double
dimension array
________________________________________________________________________________________
__________________________________
CODE:-
------
import java.util.Scanner;
public class POP
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array : ");
int rows=sc.nextInt();
int columns=sc.nextInt();
int sum=0,i,j;
int twoD[][]=new int[rows][columns];
System.out.println("Enter array elements : ");
for( i=0; i<rows;i++)
{
for( j=0; j<columns;j++)
{
twoD[i][j]=sc.nextInt();
}
}
for( i=0;i<rows;i++)
{
for(j=0;j<columns;j++){
sum+=twoD[i][j];
System.out.printf("Elements in [%d,%d] = %d\n",i,j,twoD[i][j]);
}
System.out.println("The sum of the array " +sum);
}
}
}
Output:-
-------
Enter the size of the array :
2
2
Enter array elements :
1
2
3
4
Elements in [0,0] = 1
Elements in [0,1] = 2
3
Elements in [1,0] = 3
Elements in [1,1] = 4
The sum of the array 10
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.19 Write a program to accept and Subtract two double
dimension arrays
________________________________________________________________________________________
__________________________________
CODE:-
------
import java.util.Scanner;
public class POP
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int rows, cols;
int a[][] = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
int b[][] = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Array diff will hold the result
int diff[][] = new int[rows][cols];
//Performs subtraction of matrices a and b. Store the result in matrix diff
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++){
diff[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Subtraction of two matrices: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.print(diff[i][j] + " ");
}
System.out.println();
}
}
}
Output:-
-------
Subtraction of two matrices:
2 5 3
1 1 0
0 1 2
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.20 Multiplication Of Two Matrices Using JAVA Program
________________________________________________________________________________________
__________________________________
CODE:-
------
import java.util.Scanner;
public class POP
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output:-
-------
6 6 6
12 12 12
18 18 18
Process finished with exit code 0
----------------------------------------------------------------------------------------
-------------------------------------
1.21 Write a Java program to print the frequency of elements in an
array.
CODE:-
------
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in)
;
int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};
int [] fr = new int [arr.length];
int visited = -1;
for(int i = 0; i < arr.length; i++){
int count = 1;
for(int j = i+1; j < arr.length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
//Displays the frequency of each element present in array
System.out.println("---------------------------------------");
System.out.println(" Element | Frequency");
System.out.println("---------------------------------------");
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.println(" " + arr[i] + " | " + fr[i]);
}
System.out.println("----------------------------------------");
}
}
Output:-
-------
---------------------------------------
Element | Frequency
---------------------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------------------
Process finished with exit code 0
>Good bye and
happyCoding<
At Subhasgram we provide Computer Training and Problem Solving. We always believe in deep learning policies. The more you learn more you can earn. Knowledge is the most powerful weapon in this world. If you can use it then the whole world is waiting for you. Be with us and feel the magic of deep learning. Technologica Computer Education Society (Govt.Regd).
Description
Subscribe to:
Post Comments (Atom)
JAVA PROGRAMMING - OOPS CHAPTER 1
Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...
-
Spoken English Level 4 A. COMPLEX EXPERIENCE SHARE: [All about your feelings] 1. When you experienced a quarrel. 2. When you expe...
-
Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...
No comments:
Post a Comment