prowessapps.in Java Arrays Programs

 WAP to calculate the sum of all elemets of an array .

class Test {
 void sum(int[] ar) {
 int sum = 0;
  for(int i = 0; i< ar.length; i++) {
   sum = sum + ar[i];
  }
  System.out.print("Sum = "+sum);
 }
 public static void main(String[] a)
 {
  int[] arr = {7,2,5,4,5};
  Test t = new Test();
  t.sum(arr);
 }
}
OUTPUT :
Sum = 23

 WAP to find largest number of an array .

class Test {
 void max(int[] a) {
  int max = a[0];
  for(int i = 0; i< a.length; i++) {
   if(max < a[i]) {
    max = a[i];
   }
  }
  System.out.print("Max = "+max);
 }
 public static void main(String[] a)
 {
  int[] ar = {34,23,67,45,23};
  Test t = new Test();
  t.max(ar);
 }
}
OUTPUT :
Max = 67

 WAP to find smallest element of an Array .

import java.util.Scanner;
class Test {
 void min(int[] a){
  int min = a[0];
  for(int i=0; i< a.length; i++) {
   if(min>a[i]) {
    min = a[i];
   }
  }
  System.out.print("Smallest = "+min);
 }
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Array Size: ");
  int n = s.nextInt();
  int arr[] = new int[n];
  System.out.print("Enter Elements: ");
  for(int i=0;i < n; i++) {
   arr[i] = s.nextInt();
  }
  Test t = new Test();
  t.min(arr);
 }
} 
OUTPUT :
Enter Array Size: 5
Enter Elements: 12 15 11 92 34
Smallest = 11

 WAP to find the second smallest element of an array .

import java.util.Scanner;
class Test { 
 void min() {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Size: ");
  int n = s.nextInt();
  int a[]=new int[n];
  System.out.print("Enter Element: ");
  for(int i=0; i< n; i++){
   a[i]= s.nextInt();
  }
  int min = a[0], m = 0;
  for(int i =1; i< n; i++) {
   if(a[i] < min) {
    min = a[i];
    m = i;
   }
  }
  int smin = a[n-m-1];
  for(int i =0 ; i< n; i++) {
   if(smin> a[i] && m!= i) {
    smin = a[i];
   }
  }
  System.out.print("2nd Min = "+smin);
 }
 public static void main(String[] a)
 {
  Test t = new Test();
  t.min();
 }
}  
OUTPUT :
Enter Size: 5
Enter Element: 5 7 9 6 8
2nd Min = 6

 WAP to find the second largest element of an array .

class Test {
 void max() {
  int[] a = new int[]{2,3,4,5,6};
  int max = a[0], m = 0;
  for(int i =1; i< a.length; i++) {
   if(a[i]>max) {
    max = a[i];
    m = i;
   }
  }
  int smax = a[a.length-m-1];
  for(int i =0 ; i< a.length; i++) {
   if(smax< a[i] && m!= i) {
    smax = a[i];
   }
  }
   System.out.print("2nd Max = "+smax);
 }
 public static void main(String[] a)
 {
  Test t = new Test();
  t.max();
 }
}
OUTPUT :
2nd Max = 5

 WAP to search an element in an array .

import java.util.Scanner;
class Test {
 void search(int x) {
  int a[]={1,10,20,30,40};
  int i,flag = 0;
  for(i=0; i< a.length; i++){
   if(a[i]==x){
    flag = 1;
    break;
   }
  }
  if(flag==0) {
   System.out.print("Element not Found");
  }
  else {
   System.out.print("Found at "+(i+1));
  }
 }
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Element: ");
  int x = s.nextInt();
  Test t = new Test();
  t.search(x);
 }
}
OUTPUT :
Enter Element: 30
Found at 3 


 WAP to sort an Array using bubble sort algorithm .

class Test{
 void  bubbleSort(int[] a) {
  int temp;
  for(int i = 1;i< a.length-1; i++) {
   for(int j = 0; j< a.length-i; j++) {
    if(a[j]>a[j+1]) {
     temp = a[j];
     a[j] = a[j+1];
     a[j+1] = temp;
    }
   }
  }
  for(int i=0; i< a.length; i++) {
   System.out.println(a[i]);
  }
 }
 public static void main(String[] a)
 {
  int[] arr ={10,6,8,3,5};
  Test t = new Test();
  t.bubbleSort(arr);
 }
}
OUTPUT :
3
5
6
8
10

 WAP to store the detail of 5 student and display .

import java.util.Scanner;
class Student {
 int roll;
 String name;
 Scanner s=new Scanner(System.in);
 public void input() {
  System.out.print("Enter Roll: ");
  roll=Integer.parseInt(s.nextLine());
  System.out.print("Enter Name: ");
  name = s.nextLine();
 }
 public void display() {
  System.out.println("Name- "+name);
  System.out.println("Roll- "+roll);
 }
}
class Main {
 public static void main(String[] a)
 {
  Student[] s = new Student[5];
  for(int i=0; i< s.length; i++) {
   s[i] =new Student();
   s[i].input();
  }
  for(int i=0; i< s.length; i++) {
   s[i].display();
  }
 }
}
OUTPUT :
Enter Roll: 12
Enter Name: Alok
Enter Roll: 13
Enter Name: Sumit
Enter Roll: 14
Enter Name: Ayan
Enter Roll: 15
Enter Name: Amritanshu
Enter Roll: 16
Enter Name: Rimu
Name- Alok
Roll- 12
Name- Sumit
Roll- 13
Name- Ayan
Roll- 14
Name- Amritanshu
Roll- 15
Name- Rimu
Roll- 16

 WAP to perform addition of two matrix .

import java.util.Scanner;
class Matrix {
 void add() {
  Scanner s=new Scanner(System.in);
  int m1[][] = new int[3][3];
  int m2[][] = new int[3][3];
  int m3[][] = new int[3][3];
  System.out.println("Enter Elements of Mat1: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 3; j++) { 
    m1[i][j] = s.nextInt();
   }
  }
  System.out.println("Enter Elements of Mat2: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 3; j++) { 
    m2[i][j] = s.nextInt();
   }
  }
  System.out.println("Sum of the matrix: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 3; j++) { 
    m3[i][j] = m1[i][j]+m2[i][j];
    System.out.print(m3[i][j]+"  ");
   }
   System.out.println();
  }
 }
 public static void main(String[] a)
 {
  Matrix m = new Matrix();
  m.add();
 }
}
OUTPUT :
Enter Elements of Mat1:
1 2 3
4 5 6
1 2 3
Enter Elements of Mat2:
1 3 3
1 2 3
1 3 2
Sum of the matrix:
2  5  6
5  7  9
2  5  5

 WAP to Substract two matrix .

import java.util.Scanner;
class Matrix {
 void sub() {
  Scanner s=new Scanner(System.in);
  int m1[][] = new int[3][3];
  int m2[][] = new int[3][3];
  int m3[][] = new int[3][3];
  System.out.println("Enter Elements of Mat1: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 3; j++) { 
    m1[i][j] = s.nextInt();
   }
  }
  System.out.println("Enter Elements of Mat2: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 3; j++) { 
    m2[i][j] = s.nextInt();
   }
  }
  System.out.println("Difference of the matrix: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 3; j++) { 
    m3[i][j] = m1[i][j]-m2[i][j];
    System.out.print(m3[i][j]+"  ");
   }
   System.out.println();
  }
 }
 public static void main(String[] a)
 {
  Matrix m = new Matrix();
  m.sub();
 }
}
OUTPUT :
Enter Elements of Mat1:
6 7 8
8 7 6
5 6 8
Enter Elements of Mat2:
4 5 6
3 4 4
3 4 2
Difference of the matrix:
2  2  2
5  3  2
2  2  6

 WAP to multiply two Matrix .

import java.util.Scanner;
class Matrix {
 void mul() {
  Scanner s=new Scanner(System.in);
  int r1 = 3, c1 = 2 ;
  int r2 = 2, c2 = 4, sum =0;
  int m1[][] = new int[r1][c1];
  int m2[][] = new int[r2][c2];
  int m3[][] = new int[r1][c2];
  System.out.println("Enter Elements of Mat1: ");
  for(int i = 0; i< r1; i++) {
   for(int j = 0; j< c1; j++) { 
    m1[i][j] = s.nextInt();
   }
  }
  System.out.println("Enter Elements of Mat2: ");
  for(int i = 0; i< r2; i++) {
   for(int j = 0; j< c2; j++) { 
    m2[i][j] = s.nextInt();
   }
  }
  for(int i = 0; i< r1; i++) {
   for(int j = 0; j< c2; j++) { 
    for(int k = 0; k< r2;k++) {
     sum = sum+m1[i][k]*m2[k][j];
    }
    m3[i][j]= sum;
    sum = 0;
   }
  }
  System.out.println("Product of the matrix: ");
  for(int i = 0; i< r1; i++) {
   for(int j = 0; j< c2; j++) { 
    System.out.print(m3[i][j]+"  ");
   }
   System.out.println();
  }
 }
 public static void main(String[] a)
 {
  Matrix m = new Matrix();
  m.mul();
 }
}
OUTPUT :
Enter Elements of Mat1:
1 4
2 3
4 5
Enter Elements of Mat2:
1 2 3 4
1 3 4 5
Product of the matrix:
5  14  19  24
5  13  18  23
9  23  32  41 

 WAP to display the Transpose of Matrix .

import java.util.Scanner;
class Matrix {
 void transpose() {
  Scanner s=new Scanner(System.in);
  int m1[][] = new int[3][2];
  int trans[][] = new int[2][3];
  System.out.println("Enter Elements of Mat1: ");
  for(int i = 0; i< 3; i++) {
   for(int j = 0; j< 2; j++) { 
    m1[i][j] = s.nextInt();
    trans[j][i] = m1[i][j];
   }
  }
  System.out.println("Transpose of the matrix: ");
  for(int i = 0; i< 2; i++) {
   for(int j = 0; j< 3; j++) { 
    System.out.print(trans[i][j]+"  ");
   }
   System.out.println();
  }
 }
 public static void main(String[] a)
 {
  Matrix m = new Matrix();
  m.transpose();
 }
}
OUTPUT :
Enter Elements of Mat1:
1 2
3 4
5 6
Transpose of the matrix:
1  3  5
2  4  6




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved