ICSE BlueJ Array Programs

Array Questions for Java or BlueJ

(Note: All programs codes are Tested in BlueJ. In some programs we assign some constant value to arrays.)

Questions Based on One Dimension Arrays:

1. Accept 10 numbers into an array and then calculate the sum of numbers present in odd positions and even positions respectively.
import java.io.DataInputStream;
import java.io.IOException;
public class ques1
{
  public static void arr() throws IOException
  {
    DataInputStream in  =  new DataInputStream(System.in);
    int n[] = new int[10];
    for(int i = 0;i<10;i++)
    {
      System.out.print ("Enter a number : ");
      n[i] = Integer.parseInt ( in.readLine() );
    }
    int evenSum = 0,oddSum = 0;
    for(int i = 0;i<10;i++)
    {
      if(i%2 =  = 0)
        oddSum = oddSum+n[i];
      else
        evenSum = evenSum+n[i];
    }
    System.out.println ("Sum of ODD Location : " + oddSum);
    System.out.println ("Sum of EVEN Location : " + evenSum);
  }
}

2. Accept 10 numbers into an array and then calculate the sum of even numbers present in odd positions.
import java.io.DataInputStream;
import java.io.IOException;
public class ques2
{
  public static void arr() throws IOException
  {
    DataInputStream in  =  new DataInputStream(System.in);
    int n[] = new int[10];
    for(int i = 0;i<5;i++)
    {
      System.out.print ("Enter a number : ");
      n[i] = Integer.parseInt ( in.readLine() );
    }
    int Sum = 0;
    for(int i = 0;i<10;i++)
      if(n[i]%2 =  = 0 && i%2! = 0)
        Sum = Sum+n[i];
    System.out.println ("Sum of Numbers : " + Sum);
  }
}
3. Create an array of size 10. Automatically fill the array with the factorial of number between 1 to 10, and then display the content of array.
public class ques3
{
  public static void main()
  {
    int i,f = 1;
    int x[] = new int[10];
    for(i = 0;i<10;i++)
    {
      f = f*(i+1);
      x[i] = f;
    }
    for(i = 0;i<10;i++)
      System.out.println (x[i]);
  }
}
4. Create three arrays A, B and C both of size 5. Accept numbers in two arrays A and B. Fill all the elements of array C with the sum of numbers present in appropriate element of array A and B.
public class ques4
{
  public static void main()
  {
    int i;
    int a[] = {1,2,3,4,5};
    int b[] = {6,7,8,9,10};
    int c[] = new int[5];
    for(i = 0;i<5;i++)
      c[i] = a[i]+b[i];
    for(i = 0;i<5;i++)
      System.out.println (c[i]);
  }
}
5. Create two arrays A and B of size 5 and C of size 10. Accept numbers in two arrays A and B. Fill the array C in such a way that the first five positions occupy the numbers present in array A and rest of five positions occupy the numbers present in array B.
public class ques5
{
  public static void main()
  {
    int i,j;
    int a[] = {1,2,3,4,5};
    int b[] = {6,7,8,9,10};
    int c[] = new int[10];
    for(i = 0;i<5;i++)
    {
      c[i] = a[i];
      c[i+5] = b[i];
    }
    for(i = 0;i<10;i++)
    {
      System.out.println (c[i]);
    }
  }
}
6. Create two arrays A and B of size 5 and C of size 10. Accept numbers in two arrays A and B. Fill the array C in such a way that the all odd positions occupy the numbers present in array A and all even positions occupy the numbers present in array B.
public class ques6
{
  public static void main()
  {
    int i,j;
    int a[] = {1,2,3,4,5};
    int b[] = {6,7,8,9,10};
    int c[] = new int[10];
    for(i = 0;i<5;i++)
    {
      c[i*2] = a[i];
      c[i*2+1] = b[i];
    }
    for(i = 0;i<10;i++)
      System.out.println (c[i]);
  }
}
7. Accept numbers in array A[10] & create another array B[5]. The array B first location fills with sum of first & second location of array A. The array B second location fills with sum of third & forth location of array A. Similarly fill all the position of array B & display it.
public class ques7
{
  public static void main()
  {
    int i;
    int a[] = {1,2,3,4,5,6,7,8,9,10};
    int b[] = new int[5];
    for(i = 0;i<5;i++)
      b[i] = a[i*2]+a[i*2+1];
    for(i = 0;i<5;i++)
      System.out.println (b[i]);
  }
}
8. Accept data into two integers array A & B of size 5 elements each. The program should create another array T that finds the intersection of the two arrays.
For e.g. if A  =  {1,3,5,7,8} & B  =  {7,4,2,8,9} Then T  =  {7,8}
public class ques8
{
  public static void main()
  {
    int i,j,x = 0;
    int a[] = {1,2,3,4,5};
    int b[] = {3,4,5,6,7};
    int c[] = new int[5];
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<5;j++)
      {
        if(a[i] =  = b[j])
        {
          c[x] = a[i];
          x++;
        }
      }
    }
    for(i = 0;i<x;i++)
      System.out.println (c[i]);
  }
}
9. Accept numbers into an integer array A of size 4, create another string array B of size 3. Accept any three mathematical symbols like (+, -, *, /) in array B. Then according to mathematical symbol calculate the answer.
For e.g. if A  =  [8, 6, 4, 2] & B  =  [+, -, *], then answer will be (((8 + 6) - 4) * 2)  =  20
import java.io.DataInputStream;
import java.io.IOException;
public class que9
{
  public static void main()throws IOException
  {
    DataInputStream x = new DataInputStream(System.in);
    int i;
    double s = 0.0;
    int a[] = new int[4];
    String b[] = new String[3];
    for(i = 0;i<4;i++)
    {
      System.out.print ("Enter a Number: ");
      a[i] = Integer.parseInt (x.readLine());
    }
    for(i = 0;i<3;i++)
    {
      System.out.print ("Operator[+,-,*,/]: ");
      b[i] = x.readLine();
    }
    s = a[0];
    for(i = 0;i<3;i++)
    {
      if(b[i].equals("+"))
        s = s+a[i+1];
      if(b[i].equals("*"))
        s = s*a[i+1];
      if(b[i].equals("-"))
        s = s-a[i+1];
      if(b[i].equals("/"))
        s = s/a[i+1];
    }
    System.out.println ("Result is -->"+s);
  }
}
10. Create an array of size 10 then accept 9 numbers into it. Then accept another number and its position where you want to insert it. Insert the given number in given position then Display the array.
11. Accept 10 numbers into an array. Then accept a position number and delete the number present in that position.
import java.io.DataInputStream;
import java.io.IOException;
public class ques11
{
  public static void main()throws IOException
  {
    DataInputStream x = new DataInputStream(System.in);
    int a[] = new int[10];
    int i,p;
    for(i = 0;i<10;i++)
    {
      System.out.print ("enter a number: ");
      a[i] = Integer.parseInt (x.readLine());
    }
    System.out.print ("enter position number: ");
    p = Integer.parseInt (x.readLine());
    for(i = p;i<9;i++)
      a[i] = a[i+1];
    for(i = 0;i<9;i++)
      System.out.println (a[i]);
  }
}
12. Accept the name, physics, chemistry and math marks of 25 students. The display a list of the given data with Total and Average.
import java.io.DataInputStream;
import java.io.IOException;
public class ques12
{
  public static void main()throws IOException
  {
    DataInputStream x = new DataInputStream(System.in);
    String a[] = new String[5];
    int b[] = new int[5];
    int c[] = new int[5];
    int i;
    for(i = 0;i<5;i++)
    {
      System.out.print ("enter name:");
      a[i] = x.readLine();
      System.out.print ("enter physics marks:");
      b[i] = Integer.parseInt (x.readLine());
      System.out.print ("enter chemistry marks:");
      c[i] = Integer.parseInt (x.readLine());
    }
    System.out.println ("Name \t\t Phy \t\t Chem \t\t Total \t\t Average");
    for(i = 0;i<5;i++)
    {
      int total = b[i]+c[i];
      float avg = total/2;
      System.out.println (a[i]+ "\t\t"+b[i]+"\t\t"+c[i]+ "\t\t"+total+"\t\t"+avg);
    }
  }
}
14. Write a program in Java to open 3 arrays of name A, P & N. Store 15 numbers in array A. Shift all the positive even numbers in array P and all the negative odd numbers in array N. Finally print the array P & N.
import java.io.DataInputStream;
import java.io.IOException;
public class ques14
{
  public static void main()throws IOException
  {
    DataInputStream x = new DataInputStream(System.in);
    int a[] = new int[5];
    int p[] = new int[5];
    int n[] = new int[5];
    int i,k = 0,l = 0;
    for(i = 0;i<5;i++)
    {
      System.out.print ("enter a number: ");
      a[i] = Integer.parseInt (x.readLine());
    }
    for(i = 0;i<5;i++)
    {
      if(a[i]>0 && a[i]%2 =  = 0)
      {
        p[k] = a[i];
        k++;
      }
    }
    for(i = 0;i<5;i++)
    {
      if(a[i]<0 && a[i]%2! = 0)
      {
        n[l] = a[i];
        l++;
      }
    }
    System.out.print ("List of positive numbers");
    for(i = 0;i<k;i++)
      System.out.println (p[i]);
    System.out.print ("List of negative numbers");
    for(i = 0;i<l;i++)
      System.out.println (n[i]);
  }
}
15. Write a program to find from the following data: 17, 20, 24, 29, 16, 87, 19, 52
i) The largest and smallest element b) Product of the odd numbers c) sum of the even numbers
public class ques15
{
  public static void main()
  {
    int i;
    int a[] = {17,20,24,29,16,87,19,52};
    int max = a[0],min = a[0],oddSum = 0,evenSum = 0;
    for(i = 0;i<8;i++)
    {
      if(a[i]%2 =  = 0)
        evenSum+ = a[i];
      if(a[i]%2 =  = 1)
        oddSum+ = a[i];
      if(a[i]>max)
        max = a[i];
      if(a[i]<min)
        min = a[i];
    }
    System.out.println ("Sum of Odd nos  =  "+oddSum);
    System.out.println ("Sum of Even nos  =  "+evenSum);
    System.out.println ("Maximum  =  "+max);
    System.out.println ("Minimum  =  "+min);
  }
}

Questions Based on Searching and Sorting:

13. Accept numbers into an array of size 10. Then accept a number and search that number in array. If the number is present in array, then display the array element number where number is found. In case of multiple found display all the positions. Display a proper message if the number is not present in array.
// to search for a value and show position
// sequential search or linear search
public class ques13
{
  public static void main(int number)
  {
    int i,j;
    boolean flag = false;
    int a[] = {2,6,8,5,1,4,8,0,2,7};
    for(i = 0;i<10;i++)
    {
      if(a[i] =  = number)
      {
        System.out.println ("Match found in position:"+(i+1));
        flag = true;
      }
    }
    if(flag =  = false)
      System.out.println ("Number not found in Array");
  }
}
16. Write a program to accept 10 numbers in an array and sort it.
public class bubbleSort
{
  public static void main()
  {
    int i,j,k;
    int a[] = {2,8,1,9,4,3};
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<5-i;j++)
      {
        if(a[j]>a[j+1])
        {
          k = a[j];
          a[j] = a[j+1];
          a[j+1] = k;
        }
      }
    }
    for(i = 0;i<6;i++)
      System.out.println (a[i]);
  }
}
17. Create a list of names of 25 Country's and then display name alphabetically.
public class stringSort
{
  public static void main()
  {
    String a[] = {"Germany", "Nepal", "India", "Pakistan", "Iraq"};
    String k;
    int i,j;
    for(i = 0;i<4;i++)
    {
      for(j = 0;j<4-i;j++)
      {
        if(a[j].compareTo(a[j+1])>0)
        {
          k = a[j];
          a[j] = a[j+1];
          a[j+1] = k;
        }
      }
    }
    for(j = 0;j<5;j++)
      System.out.println (a[j]);
  }
}
18. There are 100 elements in an array, Write a program in JAVA to arrange first 50 elements of the array in ascending order and rest 50 elements into descending order.
public class ques18
{
  public static void main()
  {
    int i,j,k,l;
    int a[] = {2,8,1,9,4,3,7,6,3,5};
    for(i = 0;i<5;i++)
    {
      for(j = i+1;j<6;j++)
      {
        if(a[i]>a[j])
        {
          k = a[i];
          a[i] = a[j];
          a[j] = k;
        }
      }
    }
    for(i = 5;i<9;i++)
    {
      for(j = i+1;j<10;j++)
      {
        if(a[i]<a[j])
        {
          k = a[i];
          a[i] = a[j];
          a[j] = k;
        }
      }
    }
    for(i = 0;i<10;i++)
      System.out.println (a[i]);
  }
}
19. Write a program in JAVA to accept the name and marks in computer science of forty students in an array and then print the name and marks of students according to their merit.
/* Generate the Marit List */
import java.io.DataInputStream;
import java.io.IOException;
public class ques19
{
  public static void main() throws IOException
  {
    DataInputStream stdin  =  new DataInputStream(System.in);
    String n[] = new String[5];
    int m[] = new int[5];
    int i,j,k;
    for(i = 0;i<5;i++)
    {
      System.out.print ("Enter Name : ");
      n[i] = std in.readLine() ;
      System.out.print ("Enter Comp. Sc. Marks : ");
      m[i] = Integer.parseInt (std in.readLine() );
    }
    // Sort the array in Descending order
    String x = null;
    for(i = 0;i<4;i++)
    {
      for(j = i+1;j<5;j++)
      {
        if(m[i]<m[j])
        {
          k = m[i]; m[i] = m[j]; m[j] = k;
          x = n[i]; n[i] = n[j]; n[j] = x;
        }
      }
    }
    // display Merit List
    System.out.println ("Name \t\t Marks");
    for(i = 0;i<5;i++)
      System.out.println (n[i]+"\t\t"+m[i]);
  } // end of method
}
20. Write a JAVA program to accept the temperature of any 10 cities in degrees Fahrenheit. Convert temperature to degree centigrade using the given formula: Centigrade  =  (Fahrenheit - 32) X 5/9

Display the information in the given format. Also at the end print the total number of cities where the temperature is more then 35 degree centigrade and the city name with maximum temperature.
   City Name    Fahrenheit Temperature    Centigrade Temperature
   ---------    ----------------------    ----------------------


Number of cities more then 35-degree centigrade temperature:
Name of the city with maximum temperature:

21. Write a program in JAVA that reads the following list of countries and their respective cities into two separate one-dimensional arrays. The program should accept the name of a country as input and give the name of the corresponding city as an output. The program should be designed to give an error message where a city is asked for a country whose name is not given in the list. To stop the program, "XXX" is to be entered as input.
    GERMANY       BERLIN
    NEPAL         KATMANDU
    JAPAN         TOKYO
    CANADA        MONTREAL
    IRAQ          BAGHDAD
    SRI LANKA     COLOMBO
    BRAZIL        BRAZILIA
    AUSTRALIA     PERTH
    INDIA         DELHI
    SOUTH AFRICA  PRETORIA
import java.io.DataInputStream;
import java.io.IOException;
public class ques21
{
  public static void main() throws IOException
  {
    DataInputStream stdin  =  new DataInputStream(System.in);
    String cou[] = {"Germany", "Nepal", "India", "Pakistan", "Itaq"};
    String cap[] = {"Berlin", "Katmandu", "Delhi", "Islamabad", "Baghdad"};
    int i;
    int size = cou.length;
    String search = null;
    do
    {
      boolean flag = false;
      System.out.print ("Enter Searching Name : ");
      search = std in.readLine() ;
      for(i = 0;i<size;i++)
      {
        if(search.compareTo(cou[i]) =  = 0 || search.compareTo(cap[i]) =  = 0)
        {
          System.out.println ("Capital of Country "+cou[i]+" is "+cap[i]);
          flag = true;
        }
      }
      if(flag =  = false)
        System.out.println ("Not found in list");
    }while(!search.equals("xxx"));
  } // end of method
}
22. Write a program in JAVA to store 20 names, address and phone number in three separate arrays. Depending on the user's choice either locates name or phone number and prints all the information.
23. Write a program in Java to stores names of the 10 politicians and their party to which they belong to in two separate arrays. Ask the users to enter the name of the party. Printout all the names of the politicians who belong to that party. Repeat the process according to user choice.

Questions Based on Double Dimension Arrays:

24. A metropolitan hotel has ten floors numbered from 1 to 10, each floor having fifty rooms. Using a single subscripted variables for the name of the customer and a double subscripted variable R[F][I] where F & I represents floor and room numbers. Write a program for the room allocation work. The program should automatically display the name of all customer, room and floor number.
25. Accept numbers into an array of size 3X4. Then display the array properly.
// accept & display in double dimension array //
public class ques25
{
  public static void main()
  {
    int a[][] = {{4,5,8,1}, {2,8,7,4}, {3,4,5,6}};
    int i,j;
    for(i = 0;i<3;i++)
    {
      for(j = 0;j<4;j++)
      {
        System.out.print (a[i][j]+" ");
      }
      System.out.println ();
    }
  }
}
26. Accept numbers into an array of size 4X4. Then display the diagonals and also display the sum of numbers present in the diagonal position.
// display and sum of both diagonals //
public class ques26
{
  public static void main()
  {
    int a[][] = {{4,5,8,1}, {2,8,7,4}, {3,4,5,6}, {1,2,7,8}};
    int i,j,sum = 0;
    for(i = 0;i<4;i++)
    {
      for(j = 0;j<4;j++)
      {
        if(i =  = j || (i+j) =  = 3)
        {
          System.out.print (a[i][j]+" ");
          sum+ = a[i][j];
        }
        else
          System.out.print (" ");
      }
      System.out.println ();
    }
    System.out.println ("Sum of diagonals :"+sum);
  }
}
27. Write a program to accept number into a 3X4 matrix. Display the original array. Then calculate and display the each row total.
// sum of each row total in 2D array //
public class ques27
{
  public static void main()
  {
    int a[][] = {{4,5,8,1}, {2,8,7,4}, {3,4,5,6}};
    int i,j;
    for(i = 0;i<3;i++)
    {
      for(j = 0;j<4;j++)
        System.out.print (a[i][j]+" ");
      System.out.println ();
    }
    int sum = 0;
    for(i = 0;i<3;i++)
    {
      sum = 0;
      for(j = 0;j<4;j++)
      {
        sum = sum+a[i][j];
      }
      System.out.println ("Row "+(i+1)+" total is : "+sum);
    }
  }
}
28. Write a program to accept number into a 3X4 matrix. Display the original array. Then calculate and display the each column total.
// sum of each column total in 2D array //
public class ques28
{
  public static void main()
  {
    int a[][] = {{4,5,8,1}, {2,8,7,4}, {3,4,5,6}};
    int i,j;
    for(i = 0;i<3;i++)
    {
      for(j = 0;j<4;j++)
        System.out.print (a[i][j]+" ");
      System.out.println ();
    }
    int sum = 0;
    for(j = 0;j<4;j++)
    {
      sum = 0;
      for(i = 0;i<3;i++)
      {
        sum = sum+a[i][j];
      }
      System.out.println ("Column "+(j+1)+" total is : "+sum);
    }
  }
}
29. Write a program to accept number into a 10X5 matrix. Then display the original array and the array sorted on each column.
// To display the columnwise sort of a dd matrix //
public class ques29
{
  public static void main()
  {
    int a[][] = {{2,3,4,5,3}, {2,6,8,4,2}, {6,7,1,2,3}, {4,6,8,4,2}, {7,8,3,9,5},
                    {6,7,1,2,8}, {1,6,8,4,2}, {2,6,8,4,2}, {9,7,1,2,4}, {3,8,1,5,3}};
    int i,j,k,t;
    // To print the matrix //
    System.out.println ("Original Array");
    for(i = 0;i<10;i++)
    {
      for(j = 0;j<5;j++)
        System.out.print (a[i][j]+" ");
      System.out.println ();
    }
    System.out.println ();
    // sort the array //
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<9;j++)
      {
        for(k = j+1;k<10;k++)
        {
          if(a[j][i]>a[k][i])
          {
            t = a[j][i];
            a[j][i] = a[k][i];
            a[k][i] = t;
          }
        }
      }
    }
    // to print the sorted array //
    System.out.println ("Array After Sorting");
    for(i = 0;i<10;i++)
    {
      for(j = 0;j<5;j++)
        System.out.print (a[i][j]+" ");
      System.out.println ();
    }
  }
}
30. Accept numbers into an array of size 5X5. Then display the numbers present in upper portion of the left diagonal of the matrix.
// display the upper portion of left & right diagonal //
public class ques30
{
  public static void main()
  {
    int a[][] = {{4,5,8,1,0}, {2,8,7,4,2}, {3,4,5,6,7}, {1,2,7,8,3}, {9,2,6,8,4}};
    int i,j;
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<5;j++)
      {
        if(i< = j)
          System.out.print (a[i][j]+" ");
        else
          System.out.print (" ");
      }
      System.out.println ();
    }
    System.out.println ();
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<5;j++)
      {
        if(i+j< = 4)
          System.out.print (a[i][j]+" ");
        else
          System.out.print (" ");
      }
      System.out.println ();
    }
  }
}
31. Accept numbers into an array of size 5X5. Then display the numbers present in below portion of the left diagonal of the matrix.
// display the upper portion of left & right diagonal //
public class ques31
{
  public static void main()
  {
    int a[][] = {{4,5,8,1,0}, {2,8,7,4,2}, {3,4,5,6,7}, {1,2,7,8,3}, {9,2,6,8,4}};
    int i,j;
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<5;j++)
      {
        if(i> = j)
          System.out.print (a[i][j]+" ");
        else
          System.out.print (" ");
      }
      System.out.println ();
    }
    System.out.println ();
    for(i = 0;i<5;i++)
    {
      for(j = 0;j<5;j++)
      {
        if(i+j> = 4)
          System.out.print (a[i][j]+" ");
        else
          System.out.print (" ");
      }
      System.out.println ();
    }
  }
}
32. Write a program to accept numbers into a 4X3 matrix. Display the original matrix and display the matrix in transpose form.
// Transpose a Matrix //
public class ques32
{
  public static void main()
  {
    int a[][] = {{4,5,8,1},{2,8,7,4},{3,4,5,6}};
    int i,j;
    for(i = 0;i<3;i++)
    {
      for(j = 0;j<4;j++)
        System.out.print (a[i][j]+" ");
      System.out.println ();
    }
    System.out.println ();
    for(i = 0;i<4;i++)
    {
      for(j = 0;j<3;j++)
        System.out.print (a[j][i]+" ");
      System.out.println ();
    }
  }
}
33. Write a program to accept number in two 3X4 array and then find their sum as another 3X4 array.
34. Two matrices A and B are given as:
    9 7  13               8 7  9
A  =  6 12 18           B  =  3 11 15
    5 10 15               2 9  14
Write a JAVA program to compute and print D
Where D  =  Sum of left diagonal of array A
          Sum of right diagonal of array B
 
35. Write a program to generate a Pascal triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first six rows of the triangle.
(Hints: E[J.I]  =  P[I-1,J-1] + P[I-1,J] is true for all elements but the boundary elements which are all equal to 1)
// Display the no. of line of Pascal Triangle //
public class ques39
{
  public static void main()
  {
    int size = 5;
    int a[][] = new int[size][size];
    int i,j;
    a[0][0] = 1;
    // prepare the pascal triangle //
    for(i = 1;i<size;i++)
    {
      for(j = 0;j<size;j++)
      {
        if((i =  = j) || (j =  = 0))
          a[i][j] = 1;
        else
          a[i][j] = a[i-1][j-1]+a[i-1][j];
      }
    }
    // display the pascal triangle //
    for(i = 0;i<size;i++)
    {
      for(j = 1;j< = 15-i;j++)
        System.out.print (" ");
      for(j = 0;j< = i;j++)
        System.out.print (a[i][j]+" ");
      System.out.println ();
    }
  }
}
36. Write a program to generate a Pascal triangle in which each number in the row is the sum of the two numbers immediately above it. The program should accept the number of rows to be print.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

public class pascal
{
  public static void main(int n)
  {
    int pas[] = new int[n+1];
    pas[0] = 1;
    for(int i = 0;i<n;i++)
    {
      for(int j = 0;j< = i;j++)
        System.out.print (pas[j]+" ");
      System.out.println ();
      for(int j = i+1;j>0;j--)
        pas[j] = pas[j]+pas[j-1];
    }
  }
}
37. Write a program to perform MATRIX multiplication of two Matrixes. Where size of two matrixes is given by user, also check MATRIX multiplication possible or not.
/* Multiply two Matrices */
import java.io.DataInputStream;
import java.io.IOException;
public class ques37
{
  public static void main() throws IOException
  {
    DataInputStream in  =  new DataInputStream(System.in);
    int i,j,k,m,n,p,q;
    System.out.print ("Enter Size of First Array : ");
    m = Integer.parseInt ( in.readLine() );
    n = Integer.parseInt ( in.readLine() );
    System.out.print ("Enter Size of Second Array : ");
    p = Integer.parseInt ( in.readLine() );
    q = Integer.parseInt ( in.readLine() );
    if(n =  = p)
    {
      int a[][] = new int[m][n];
      int b[][] = new int[p][q];
      int c[][] = new int[m][q];
      // Accept numbers in First Matrix
      for(i = 0;i<m;i++)
      {
        for(j = 0;j<n;j++)
        {
          System.out.print ("Enter Nos. in First Array : ");
          a[i][j] = Integer.parseInt ( in.readLine() );
        }
      }
      // Accept numbers in Second Matrix
      for(i = 0;i<p;i++)
      {
        for(j = 0;j<q;j++)
        {
          System.out.print ("Enter Nos. in Second Array : ");
          b[i][j] = Integer.parseInt ( in.readLine() );
        }
      }
      // Multiply two Matrices
      int s = 0;
      for(i = 0;i<m;i++)
      {
        for(j = 0;j<q;j++)
        {
          s = 0;
          for(k = 0;k<n;k++)
          {
            s = s+a[i][k]*b[k][j];
          }
          c[i][j] = s;
        }
      }
      // Display the content of resultent Matrix
      for(i = 0;i<m;i++)
      {
        for(j = 0;j<q;j++)
        {
           System.out.print (c[i][j]+" ");
        }
        System.out.println ();
      }
    }
    else
      System.out.print ("Matrix Multiplication not possible");
  }
}