Gr10 Comp. App. Record Programs
Gr10 Comp. App. Record Programs
/* Write a Java Program to input 10 numbers into an array and print them out. */
import java.util.*;
public class Program1
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[] arr = new int[10];
        System.out.println(“Enter 10 integers: “);
        for(int i=0; i<10; i++)
        {
             arr[i] = key.nextInt();
        }
        System.out.println(“The integers are: “);
        for(int j=0; j<10; j++)
        {
             System.out.print(arr[j] + “, “);
        }
    }
}
Variable Listing:
Program 2:
/* Write a Java Program to input 10 numbers into an array and print them in reverse order. */
import java.util.*;
public class Program2
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[] arr = new int[10];
        System.out.println("Enter 10 integers: ");
        for(int i=0; i<10; i++)
        {
             arr[i] = key.nextInt();
        }
        System.out.println("The integers in reverse are: ");
        for(int j=10-1; j>=0; j--)
        {
             System.out.print(arr[j] + ", ");
        }
    }
}
Variable Listing:
Program 3:
/* Write a Java Program to input 10 numbers into an array and print the values of those cells whose
subscripts are odd. */
import java.util.*;
public class Program3
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[] arr = new int[10];
        System.out.println("Enter 10 integers: ");
        for(int i=0; i<10; i++)
        {
             arr[i] = key.nextInt();
        }
        System.out.println("The cells with odd subscripts are: ");
        for(int j=0; j<10; j++)
        {
             if(j%2!=0)
             {
                  System.out.print(arr[j] + ", ");
             }
        }
    }
}
Variable Listing:
Program 4:
/* Write a Java Program to initialize an array of 5 elements and print their sum and average. */
public class Program4
{
    public static void main()
    {
        int[] arr = new int[] {1, 2, 3, 4, 5};
        double sum=0;
        double average=0;
        for(int i=0; i<arr.length; i++)
        {
             sum+=arr[i];
        }
        average=sum/arr.length;
        System.out.println("The sum of the elements is " + sum + " and the average is " + average);
    }
}
Variable Listing:
Program 5:
/* Write a Java Program to input 20 numbers into an array and print the even elements. */
import java.util.*;
public class Program5
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter 20 integers:");
        int[] arr=new int[20];
        for(int i=0; i<20; i++)
        {
             arr[i] = key.nextInt();h
        }
        System.out.println("The even elements are:");
        for(int j=0; j<20; j++)
        {
             if(arr[j]%2==0)
             {
                  System.out.print(arr[j] + ", ");
             }
        }
    }
}
Variable Listing:
        Variable              Datatype                                       Description
Program 6:
/* Write a Java Program to accept values into 2 arrays of 10 cells each. Store the sum of the corresponding
cells in a third array. Display the resultant array. */
import java.util.*;
public class Program6
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[] arr1=new int[10];
        int[] arr2=new int[10];
        System.out.println("Enter 10 integers into array 1:");
        for(int i=0; i<10; i++)
        {
             arr1[i]=key.nextInt();
        }
        System.out.println("Enter 10 integers into array 2:");
        for(int j=0; j<10; j++)
        {
             arr2[j]=key.nextInt();
        }
        int[] sum=new int[10];
        for(int k=0; k<10; k++)
        {
             sum[k] = arr1[k] + arr2[k];
        }
        System.out.println("The resultant array is:");
        for(int l=0; l<10; l++)
        {
             System.out.print(sum[l] + ", ");
        }
     }
}
Variable Listing:
i int to cycle through every index of the first array (for input)
j int to cycle through every index of the second array (for input)
               sum                      int[]             to store the sums of the corresponding integer elements in the
                                                                              first and second arrays
                k                       int                 to store the index of every element in the first, second and
                                                                                  resultant arrays
l int to cycle through every index of the resultant array (for output)
Program 7:
/*Two single dimensional arrays contain elements as follows:
X[10] = 5, -3, -2, 1, 0, 12, 14, 16, 25, 13
Y[8] = 6, 5, 10, 15, 18, 20, 22, 30
Write a Java program that results in the third array Z[] as follows:
Z[18] = 5, 6, -3, 5, -2, 10, 1, 15, 0, 18, 12, 20, 14, 22, 16, 30, 25, 13
*/
public class Program7
{
         public static void main()
         {
               int[] X=new int[] {5, -3, -2, 1, 0, 12, 14, 16, 25, 13};
               int[] Y=new int[] {6, 5, 10, 15, 18, 20, 22, 30};
               int[] Z=new int[X.length + Y.length];
               int count_x=0;
               int count_y=0;
               int count_z=0;
               for(int i=0; i<2*Math.min(X.length, Y.length); i++)
          {
                  if(i%2==0)
                  {
                       Z[count_z++] = X[count_x++];
                  }
                  else if(i%2==1)
                  {
                       Z[count_z++] = Y[count_y++];
                  }
          }
          if(X.length>Y.length)
          {
                  for(int j=Y.length; j<X.length; j++)
                  {
                       Z[count_z++]=X[j];
                  }
          }
          else if(Y.length>X.length)
          {
                  for(int k=X.length; k<Y.length; k++)
                  {
                       Z[count_z++]=Y[k];
                  }
          }
Variable Listing:
l int to cycle through the index of every element in Z[] (for output)
Program 8:
/*Write a Java Program to enter marks and names of 10 students and print it in a formatted tabular
manner. */
import java.util.*;
public class Program8
{
     public static void main()
     {
          Scanner key=new Scanner(System.in);
          String[] names=new String[10];
          int[] marks=new int[10];
          for(int i=0; i<names.length; i++)
          {
                  System.out.println("Enter name:");
                  names[i]=key.next();
                  System.out.println("Enter marks (/500):");
                  marks[i]=key.nextInt();
                  key.nextLine();
          }
          System.out.println("Name:\t\tMarks:");
          for(int j=0; j<names.length; j++)
          {
                  System.out.println(names[j] + "\t\t" + marks[j]);
          }
     }
}
Program 9:
/*Write a Java Program to store a list of roll numbers and total marks (/500) of 35 students of a class. Print
the maximum of the total marks along with the roll number of the student who attained it.
*/
import java.util.*;
public class Program9
{
     public static void main()
     {
          Scanner key=new Scanner(System.in);
          int[] roll_nos=new int[10];
          int[] marks=new int[10];
          for(int i=0; i<roll_nos.length; i++)
          {
               System.out.println("Enter roll number:");
               roll_nos[i]=key.nextInt();
               System.out.println("Enter marks (/500):");
               marks[i]=key.nextInt();
          }
          int max_marks=0;
          int index=0;
          for(int j=0; j<marks.length; j++)
          {
               if(max_marks<marks[j])
               {
                      max_marks=Math.max(max_marks, marks[j]);
                      index=j;
               }
          }
          System.out.println("The highest mark was " + max_marks + " attained by roll number " +
roll_nos[index]);
     }
}
Program 10:
/* Write a Java Program to initialize an array with the following values:
     a[] = {6, 2, 3, 4, 1, 7, 8, 5}
     Accept a value from the user and using linear search print the cell position it occupies in the array.
*/
import java.util.*;
public class Program10
{
       public static void main()
       {
               Scanner key=new Scanner(System.in);
               System.out.println("Enter an integer:");
               int search=key.nextInt();
               int[] a=new int[] {6, 2, 3, 4, 1, 7, 8, 5};
               for(int i=0; i<a.length; i++)
               {
                    if(search==a[i])
                    {
                          System.out.println(search + " occupies the cell position " + (i+1));
                          break;
                    }
               }
       }
}
Program 11:
/*Write a Java Program to enter the names of 10 students in an array and ask the user to enter a name.
Locate that particular student using linear search. */
import java.util.*;
public class Program11
{
      public static void main()
      {
           Scanner key=new Scanner(System.in);
           String[] names=new String[10];
           System.out.println("Enter 10 names:");
           for(int i=0; i<names.length; i++)
           {
                   names[i]=key.nextLine();
           }
        System.out.println("Enter the name to be searched:");
        String search=key.nextLine();
        int index=0;
        boolean found=false;
        for(int j=0; j<names.length; j++)
        {
             if(names[j].compareTo(search)==0)
             {
                 found=true;
                 index=j;
                 break;
             }
        }
        System.out.println(search + (found?" occupies the cell position " + (index + 1): " is not in the array"));
    }
}
Program 12:
/* Write a Java Program to initialize an array of 10 numeric elements in ascending order. Accept a value
from the user and using binary search, print the cell position that the number occupies in the array. */
import java.util.*;
public class Program12
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[] arr=new int[] {2, 3, 7, 9, 12, 15, 18, 19, 22, 27};
        System.out.println("Enter an integer to be searched for:");
        int search=key.nextInt();
        int LL=0;
        int UL=arr.length-1;
        int m=0;
        boolean found=false;
        while(LL<=UL)
        {
             m=(LL+UL)/2;
             if(search>arr[m])
             {
                 LL=m+1;
              }
              else if(search<arr[m])
              {
                  UL=m-1;
              }
              else if(search==arr[m])
              {
                  found=true;
                  break;
              }
          }
          System.out.println(search + (found?" occupies the cell position " + (m+1):" is not in the array"));
    }
}
Program 13:
/*Write a Java Program to initialize a sorted array of 10 strings. Accept a value from the user, and using
binary search method, print the position the string occupies in the array. */
import java.util.*;
public class Program13
{
    public static void main()
    {
          Scanner key=new Scanner(System.in);
          String[] arr=new String[] {"ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI", "HIJ", "IJK",
"JKL"};
          System.out.println("Enter a string to be searched for:");
          String search=key.nextLine();
          int LL=0;
          int UL=arr.length-1;
          int m=0;
          boolean found=false;
          while(LL<=UL)
          {
              m=(UL+LL)/2;
              if(search.compareTo(arr[m])>0)
              {
                  LL=m+1;
              }
             else if(search.compareTo(arr[m])<0)
             {
                 UL=m-1;
             }
             else if(search.compareTo(arr[m])==0)
             {
                 found=true;
                 break;
             }
        }
        System.out.println(search + (found?" occupies the cell position " + (m+1):" is not in the array"));
    }
}
Program 14:
/* Write a Java Program to initialize an array of 5 numeric elements and print the array in ascending order
using selection sort. */
public class Program14
{
    public static void main()
    {
        int[] arr=new int[] {18, 22, 7, 12, 9};
        int min=0;
        int pos=0;
        int temp=0;
        for(int i=0; i<arr.length; i++)
        {
             min=arr[i];
             pos=i;
             for(int j=i+1; j<arr.length; j++)
             {
                 if(arr[j]<min)
                 {
                      min=arr[j];
                      pos=j;
                 }
             }
             temp=arr[pos];
             arr[pos]=arr[i];
            arr[i]=temp;
        }
        System.out.println("The array in ascending order is:");
        for(int k=0; k<arr.length; k++)
        {
            System.out.print(arr[k] + ", ");
        }
    }
}
Program 15:
/* Write a Java Program to initialize an alphanumeric array of 10 elements and print the array in
descending order using selection sort. */
public class Program15
{
    public static void main()
    {
        String[] arr=new String[] {"B2", "D4", "A1", "J10", "E5", "I9", "H8", "G7", "C3", "F6"};
        String max="";
        int pos=0;
        String temp="";
        for(int i=0; i<arr.length; i++)
        {
            max=arr[i];
            pos=i;
            for(int j=i+1; j<arr.length; j++)
            {
                 if(max.compareTo(arr[j])<0)
                 {
                     max=arr[j];
                     pos=j;
                 }
            }
            temp=arr[pos];
            arr[pos]=arr[i];
            arr[i]=temp;
        }
        System.out.println("The array in descending order is:");
        for(int k=0; k<arr.length; k++)
           {
               System.out.print(arr[k] + ", ");
           }
    }
}
Program 16:
/* Write a Java Program to input 5 vowels into an array and print it out in ascending order using bubble
sort. */
import java.util.*;
public class Program16
{
    public static void main()
    {
           Scanner key=new Scanner(System.in);
           char[] arr=new char[5];
           System.out.println("Enter 5 vowels:");
           for(int k=0; k<arr.length; k++)
           {
               arr[k]=key.next().charAt(0);
           }
           char temp=' ';
           for(int i=0; i<arr.length; i++)
           {
               for(int j=0; j<arr.length-1-i; j++)
               {
                    if(arr[j]>arr[j+1])
                    {
                        temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
               }
           }
           System.out.println("The array in ascending order is:");
           for(int l=0; l<arr.length; l++)
           {
               System.out.print(arr[l] + ", ");
           }
    }
}
Program 17:
/* Write a Java Program to initialize 10 names into an array and print them out in alphabetical order
using bubble sort. */
public class Program17
{
    public static void main()
    {
        String[] names=new String[] {"MNO", "ABC", "YZA", "STU", "JKL", "GHI", "VWX", "Vishnu",
"DEF", "PQR"};
        String temp=" ";
        for(int i=0; i<names.length; i++)
        {
             for(int j=0; j<names.length-1-i; j++)
             {
                 if(names[j].compareToIgnoreCase(names[j+1])>0)
                 {
                        temp=names[j];
                        names[j]=names[j+1];
                        names[j+1]=temp;
                 }
             }
        }
        System.out.println("The names in alphabetical order are:");
        for(int k=0; k<names.length; k++)
        {
             System.out.print(names[k] + ", ");
        }
    }
}
Program 18:
/* Write a Java Program to input the marks of 7 students into an array, sort the array in descending order
using bubble sort and print it. */
import java.util.*;
public class Program18
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[] marks=new int[7];
        System.out.println("Enter the marks of 7 students: (/500)");
        for(int k=0; k<marks.length; k++)
        {
             marks[k]=key.nextInt();
        }
        int temp=0;
        for(int i=0; i<marks.length; i++)
        {
             for(int j=0; j<marks.length-1-i; j++)
             {
                 if(marks[j] < marks[j+1])
                 {
                      temp = marks[j];
                      marks[j] = marks[j+1];
                      marks[j+1] = temp;
                 }
             }
        }
        System.out.println("The marks in descending order are:");
        for(int l=0; l<marks.length; l++)
        {
             System.out.print(marks[l] + ", ");
        }
    }
}
Program 19:
/* Write a Java Program to enter 10 characters into a character array and print the array in ascending
order using bubble sort. */
import java.util.*;
public class Program19
{
    public static void main()
    {
         Scanner key=new Scanner(System.in);
         char[] arr=new char[10];
         System.out.println("Enter 10 characters:");
         for(int k=0; k<arr.length; k++)
         {
             arr[k]=key.next().charAt(0);
         }
         char temp=' ';
         for(int i=0; i<arr.length; i++)
         {
             for(int j=0; j<arr.length - 1 - i; j++)
             {
                  if(arr[j]>arr[j+1])
                  {
                      temp=arr[j];
                      arr[j]=arr[j+1];
                      arr[j+1]=temp;
                  }
             }
         }
         System.out.println("The array in ascending order is:");
         for(int l=0; l<arr.length; l++)
         {
             System.out.print(arr[l] + ", ");
         }
     }
}
Program 20:
/* Write a Java Program to enter the names of 10 countries along with their corresponding capitals in 2
single dimensional arrays. Accept the value of a country from the user and print its corresponding capital.
*/
import java.util.*;
public class Program20
{
     public static void main()
     {
         Scanner key=new Scanner (System.in);
         String[] countries=new String[10];
        String[] capitals=new String[10];
        for(int i=0; i<countries.length; i++)
        {
            System.out.println("Enter the name of a country:");
            countries[i]=key.nextLine();
            System.out.println("Enter its corresponding capital:");
            capitals[i]=key.nextLine();
        }
        System.out.println("Enter a country to output its capital:");
        String search =key.nextLine();
        boolean found=false;
        int index=0;
        for(int j=0; j<countries.length; j++)
        {
            if(countries[j].compareTo(search)==0)
            {
                 found=true;
                 index=j;
                 break;
            }
        }
        System.out.println(found?search + "'s capital is: " + capitals[index]:search + " is not in the array");
    }
}
Program 21:
/* Write a Java Program to initialize 9 numbers into a 2-Dimensional array and print it out in a matrix
format using tabs. */
public class Program21
{
    public static void main()
    {
        int[][] arr=new int[][] {{2,4,7}, {1,9,8}, {5,3,6}};
        System.out.println("The array in matrix format is:");
        for(int i=0; i<arr.length; i++)
        {
            for(int j=0; j<arr[i].length; j++)
            {
                 System.out.print(arr[i][j] + "\t");
             }
             System.out.println();
        }
    }
}
Program 22:
/* Write a Java Program to initialize 9 characters into a 2-Dimensional array and print the array in
reverse order in a matrix format. */
public class Program22
{
    public static void main()
    {
        char[][] arr=new char[][] {{'a', 'j', 'x'}, {'f', 'u', 'v'}, {'s', 'i', 'h'}};
        System.out.println("The array in reverse order is:");
        for(int i=arr.length-1; i>=0; i--)
        {
             for(int j=arr[i].length-1; j>=0; j--)
             {
                  System.out.print(arr[i][j] + "\t");
             }
             System.out.println();
        }
    }
}
Program 23:
/* Write a Java Program to enter 9 numbers into a 2-Dimensional array and print a matrix displaying only
the even numbers of the array. */
import java.util.*;
public class Program23
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        int[][] arr=new int[3][3];
        for(int i=0; i<arr.length; i++)
        {
                   for(int j=0; j<arr[i].length; j++)
                   {
                       System.out.println("Enter the element at: " + "(" + i + "," + j + ")" );
                       arr[i][j]=key.nextInt();
                   }
              }
              System.out.println("The matrix with only the even numbers is:");
              for(int k=0; k<arr.length; k++)
              {
                   for(int l=0; l<arr.length; l++)
                   {
                       if(arr[k][l]%2==0)
                       {
                              System.out.print(arr[k][l] + "\t");
                       }
                       else
                       {
                              System.out.print("*\t");
                       }
                   }
                   System.out.println();
              }
        }
}
Program 24:
/* Write a Java Program to enter numbers in a 2-Dimensional array with n rows and m columns and print
it in a matrix format. */
import java.util.*;
public class Program24
{
    public static void main()
    {
            Scanner key=new Scanner(System.in);
            System.out.println("Enter the number of rows in the matrix:");
            int n=key.nextInt();
            System.out.println("Enter the number of columns in the matrix:");
            int m=key.nextInt();
            int[][] arr=new int[n][m];
        for(int i=0; i<n; i++)
        {
                for(int j=0; j<m; j++)
                {
                       System.out.println("Enter the element at: " + "(" + i + "," + j + ")");
                       arr[i][j]=key.nextInt();
                }
        }
        System.out.println("The matrix is:");
        for(int k=0; k<n; k++)
        {
            for(int l=0; l<m; l++)
                {
                       System.out.print(arr[k][l] + "\t");
                 }
            System.out.println();
        }
    }
}
Program 25:
/* Write a Java Program to enter numbers into a 2-Dimensional array with n rows and m columns and
print the sum of the elements in each row. */
import java.util.*;
public class Program25
{
    public static void main()
    {
            Scanner key=new Scanner(System.in);
            System.out.println("Enter the number of rows in the matrix:");
            int n=key.nextInt();
            System.out.println("Enter the number of columns in the matrix:");
            int m=key.nextInt();
            int[][] arr=new int[n][m];
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                     System.out.println("Enter the element at: " + "(" + i + "," + j + ")");
                    arr[i][j]=key.nextInt();
                }
        }
        int sum=0;
        for(int k=0; k<n; k++)
        {
                sum=0;
                for(int l=0; l<m; l++)
                {
                    sum+=arr[k][l];
                }
                System.out.println("The sum of the elements in row " + (k+1) + " is: " + sum);
        }
    }
}
Program 26:
/* Write a Java Program to enter numbers into a 2-Dimensional array with n rows and m columns, store
the sum of the elements in each column in an array and print it. */
import java.util.*;
public class Program26
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter the number of rows in the matrix:");
        int n=key.nextInt();
        System.out.println("Enter the number of columns in the matrix:");
        int m=key.nextInt();
        int[][] arr=new int[n][m];
        for(int i=0; i<n; i++)
        {
                for(int j=0; j<m; j++)
                {
                    System.out.println("Enter the element at:" + "(" + i + "," + j + ")");
                    arr[i][j]=key.nextInt();
                }
            }
        int sum=0;
        int[] columns=new int[m];
        for(int k=0; k<m; k++)
        {
             sum=0;
             for(int l=0; l<n; l++)
             {
                    sum+=arr[l][k];
             }
             columns[k]=sum;
        }
        System.out.println("The sum of the columns in the array are:");
        for(int o=0; o<columns.length; o++)
        {
             System.out.print(columns[o] + ", ");
        }
    }
}
Program 27:
/* Write a Java Program to enter numbers into a 2-Dimensional array with n rows and m columns and
print the sum of the left and right diagonal elements. */
import java.util.*;
public class Program27
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter the number of rows in the matrix:");
        int n=key.nextInt();
        System.out.println("Enter the number of columns in the matrix:");
        int m=key.nextInt();
        int[][] arr=new int[n][m];
        if(m!=n)
        {
             System.out.println("Error - The matrix should be a square");
                System.exit(0);
        }
        for(int i=0; i<n; i++)
        {
                 for(int j=0; j<m; j++)
                 {
                     System.out.println("Enter the element at: " + "(" + i + "," + j + ")");
                     arr[i][j]=key.nextInt();
                 }
             }
             int l_sum=0;
             int r_sum=0;
             for(int k=0; k<n; k++)
             {
                 for(int l=0; l<m; l++)
                 {
                     if(l==k)
                     {
                          l_sum+=arr[k][l];
                     }
                     if(l+k==m-1)
                     {
                          r_sum+=arr[k][l];
                     }
                 }
             }
             System.out.println("The sum of the elements in the left diagonal is: " + l_sum);
             System.out.println("The sum of the elements in the right diagonal is: " + r_sum);
     }
}
Program 28:
/* Write a Java Program to enter numbers into a 2-Dimensional array with n rows and m columns and
print the upper-left half of the array.
If a[] is:
2 3 4 5 0
3 1 4 7 1
6 7 8 9 3
5 8 2 1 0
2 4 5 7 1
The output will be:
2 3 4 5 0
3 1 4 7
6 7 8
5 8.
2
*/
import java.util.*;
public class Program28
{
     public static void main()
     {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the number of rows in the matrix:");
         int n=key.nextInt();
         System.out.println("Enter the number of columns in the matrix:");
         int m=key.nextInt();
         int[][] arr=new int[n][m];
         if(m!=n)
         {
             System.out.println("Error - The matrix should be a square");
             System.exit(0);
         }
         for(int i=0; i<arr.length; i++)
         {
             for(int j=0; j<arr[i].length; j++)
             {
                    System.out.println("Enter the element at: " + "(" + i + "," + j + ")");
                    arr[i][j]=key.nextInt();
             }
         }
         System.out.println("The upper-left half of the array is:");
         for(int k=0; k<arr.length; k++)
         {
             for(int l=0; l<arr[k].length; l++)
             {
                    if(k+l<n)
                    {
                           System.out.print(arr[k][l] + "\t");
                    }
                    else
                    {
                           System.out.print("\t");
                   }
             }
             System.out.println();
        }
    }
}
Program 29:
/* Write a Java Program to enter 9 names into a 2-Dimensional array with n rows and m columns and
print the left diagonal elements. */
import java.util.*;
public class Program29
{
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter the number of rows in the matrix:");
        int n=key.nextInt();
        System.out.println("Enter the number of columns in the matrix:");
        int m=key.nextInt();
        String[][] names=new String[n][m];
        if(m!=n)
        {
             System.out.println("Error - The matrix should be a square");
             System.exit(0);
        }
        for(int i=0; i<names.length; i++)
        {
             for(int j=0; j<names[i].length; j++)
             {
                   System.out.println("Enter the name at: " + "(" + i + "," + j + ")");
                   names[i][j]=key.next();
             }
        }
        System.out.println("The left diagonal elements are:");
        for(int k=0; k<names.length; k++)
        {
             for(int l=0; l<names[k].length; l++)
             {
                      if(k==l)
                      {
                             System.out.print(names[k][l] + "\t");
                      }
                      else
                      {
                             System.out.print("\t");
                      }
                  }
                  System.out.println();
          }
    }
}
Variable Listing:
              i                         int             to store the index of every 1-D array inside names[] [row index]
                                                                                      (for input)
              j                         int               to store the index of every element in every 1-D array inside
                                                                          names[] [column index] (for input)
              k                         int             to store the index of every 1-D array inside names[] [row index]
                                                                                     (for output)
              l                         int               to store the index of every element in every 1-D array inside
                                                                         names[] [column index] (for output)
Program 30:
/* Write a Java Program to enter the names of 5 students into an array and the marks obtained in another
array. Display the highest marks obtained, along with the name of the students. */
import java.util.*;
public class Program30
{
    public static void main()
    {
          Scanner key=new Scanner(System.in);
          String[] names=new String[5];
          int[] marks=new int[names.length];
          for(int i=0; i<names.length; i++)
          {
                  System.out.println("Enter the name of the student:");
                  names[i]=key.nextLine();
                  System.out.println("Enter the student's marks:");
                  marks[i]=key.nextInt();
          }
          int max=0;
          int pos=0;
          for(int j=0; j<names.length; j++)
          {
                  if(max<marks[j])
                  {
                      max=marks[j];
                      pos=j;
                  }
          }
          System.out.println("The highest marks was " + max + " attained by " + names[pos]);
    }
}
Variable Listing:
i int to store the index of every element in names[] / marks[] (for output)
pos int to store the index of the current maximum mark in marks[]
             i                         int            to store the index of every element in names[] / every 1-D array
                                                                           in marks[][] (for input)
          sum                          int            to store the sum of every 1-D array in marks[][] [to store the sum
                                                                          of each student’s marks]
             l                         int              to store the index of every element in every 1-D array inside
                                                                                  marks[][]
Program 32:
/* Write a Java Program to enter the names of cities into a city array and the highest and lowest
temperatures recorded in those cities into a 2-Dimensional temperature array. Display the city
corresponding to the highest temperature and lowest temperature. */
import java.util.*;
public class Program32
{
    public static void main()
    {
         Scanner key=new Scanner(System.in);
         String[] cities=new String[10];
         double[][] temperatures=new double[10][2];
         for(int i=0; i<cities.length; i++)
         {
                 System.out.println("Enter the name of a city:");
                 cities[i]=key.nextLine();
                 for(int j=0; j<temperatures[i].length; j++)
                 {
                     if (j == 0) {
                     System.out.println("Enter the city's highest temperature in ºC:");
                } else {
                     System.out.println("Enter the city's lowest temperature in ºC:");
                }
                temperatures[i][j]=key.nextDouble();
            }
            key.nextLine();
        }
        double max_temp=-1000.0;
        int max_pos=0;
        double min_temp=1000.0;
        int min_pos=0;
        for(int k=0; k<2; k++)
        {
            for(int l=0; l<cities.length; l++)
            {
                if(temperatures[l][k]>max_temp)
                {
                     max_temp=temperatures[l][k];
                     max_pos=l;
                }
                else if(temperatures[l][k]<min_temp)
                {
                     min_temp=temperatures[l][k];
                     min_pos=l;
                }
            }
        }
        System.out.println("The city with the highest recorded temperature was: " + cities[max_pos] + " with " +
max_temp + "ºC");
        System.out.println("The city with the lowest recorded temperature was: " + cities[min_pos] + " with " +
min_temp + "ºC");
    }
}
Variable Listing:
temperatures double[][] to store 2 temperatures (max and min) per city in a 2-D array
            i                       int       to store the index of every element in cities[] / every 1-D array in
                                                                  temperatures[][] (for input)
max_pos int to store the row index of the highest element in temperatures[][0]
min_pos int to store the row index of the lowest element in temperatures[][1]
            k                       int         to store the index of every element in every 1-D array inside
                                                               temperatures[][] [column index]
            l                       int       to store the index of every 1-D array inside temperatures[][] [row
                                                                            index]
Program 33:
/* Write a Java Program to enter marks obtained by 10 students in 3 subjects (/100) [using 2-Dimensional
array] and calculate the grades of each student according to the average marks:
     Avg. Marks           Grade
     <=45.0               D
     >45.0 & <=60.0        C
     >60.0 & <=75.0        B
     >75.0                A
*/
import java.util.*;
public class Program33
{
     public static void main()
     {
Scanner key=new Scanner(System.in);
String[] names=new String[10];
double[][] marks=new double[10][3];
for(int i=0; i<names.length; i++)
{
    System.out.println("Enter the name of student " + i + ":");
    names[i]=key.nextLine();
    for(int j=0; j<marks[i].length; j++)
    {
        System.out.println("Enter the marks obtained in subject " + j + ":");
        marks[i][j]=key.nextDouble();
    }
    key.nextLine();
}
double sum=0;
double average=0;
char grade='D';
System.out.println("Names\t\tAverage\t\tGrade");
for(int k=0; k<names.length; k++)
{
    sum=0;
    for(int l=0; l<marks[k].length; l++)
    {
        sum+=marks[k][l];
    }
    average=sum/3;
Variable Listing:
              i                         int             to store the index of every element in names[] / every 1-D array
                                                                              in marks[][] (for input)
             sum                       float            to store the sum of every 1-D array in marks[][] [to store the sum
                                                                             of each student’s marks]
average float to store the average of the sum of each student’s marks
              k                         int             to store the index of every 1-D array inside marks[][] [row index]
                                                                                    (for output)
              l                         int               to store the index of every element in every 1-D array inside
                                                                    temperatures[][] [column index] (for output)
Program 34:
/* Write a Java Program to print the table of 3. [without return, without parameters] */
public class Program34
{
    public static void table()
    {
         for(int i=1; i<=10; i++)
         {
                  System.out.println(i + " * 3 " + "= " + (i*3));
         }
    }
    public static void main()
    {
         table();
    }
}
Variable Listing:
Program 35:
/* Write a Java Program using a menu to add, subtract, multiply and divide 2 numbers. [without return,
without parameters] */
import java.util.*;
public class Program35
{
    public static void add()
    {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the first number:");
         double n1=key.nextDouble();
         System.out.println("Enter the second number:");
         double n2=key.nextDouble();
         System.out.println(n1 + " + " + n2 + " = " + (n1+n2));
    }
    public static void subtract()
    {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the first number:");
         double n1=key.nextDouble();
         System.out.println("Enter the second number:");
         double n2=key.nextDouble();
         System.out.println(n1 + " - " + n2 + " = " + (n1-n2));
    }
    public static void multiply()
    {
         Scanner key=new Scanner(System.in);
        System.out.println("Enter the first number:");
        double n1=key.nextDouble();
        System.out.println("Enter the second number:");
        double n2=key.nextDouble();
        System.out.println(n1 + " * " + n2 + " = " + (n1*n2));
    }
    public static void divide()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter the first number:");
        double n1=key.nextDouble();
        System.out.println("Enter the second number:");
        double n2=key.nextDouble();
        System.out.println(n1 + " / " + n2 + " = " + (n1/n2));
    }
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter + to add 2 numbers, - to subtract 2 numbers, * to multiply 2 numbers and / to
divide 2 numbers:");
        char choice=key.next().charAt(0);
        switch(choice)
        {
            case '+':
            {
                 add();
            }
            break;
            case '-':
            {
                 subtract();
            }
            break;
            case '*':
            {
                 multiply();
            }
            break;
            case '/':
            {
                   divide();
              }
              break;
              default:
              {
                   System.out.println("Error - Invalid Input");
              }
         }
    }
}
Variable Listing:
Program 36:
/* Write a Java Program using a menu to add, subtract, multiply and divide 2 numbers. [with return,
without parameters] */
import java.util.*;
public class Program36
{
    public static double add()
    {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the first number:");
         double n1=key.nextDouble();
         System.out.println("Enter the second number:");
         double n2=key.nextDouble();
         return(n1+n2);
    }
    public static double subtract()
    {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the first number:");
         double n1=key.nextDouble();
         System.out.println("Enter the second number:");
         double n2=key.nextDouble();
         return(n1-n2);
    }
    public static double multiply()
    {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the first number:");
         double n1=key.nextDouble();
         System.out.println("Enter the second number:");
         double n2=key.nextDouble();
         return(n1*n2);
    }
    public static double divide()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter the first number:");
        double n1=key.nextDouble();
        System.out.println("Enter the second number:");
        double n2=key.nextDouble();
        return(n1/n2);
    }
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter + to add 2 numbers, - to subtract 2 numbers, * to multiply 2 numbers and / to
divide 2 numbers:");
        char choice=key.next().charAt(0);
        switch(choice)
        {
            case '+':
            {
                 System.out.println("The sum is " + add());
            }
            break;
            case '-':
            {
                 System.out.println("The difference is " + subtract());
            }
            break;
            case '*':
            {
                 System.out.println("The product is " + multiply());
            }
            break;
            case '/':
            {
                 System.out.println("The quotient is " + divide());
            }
            break;
            default:
            {
                 System.out.println("Error - Invalid Input");
              }
         }
    }
}
Variable Listing:
Variable Listing:
Program 38:
/* Write a Java Program using a menu to add, subtract, multiply and divide 2 numbers. [without return,
with parameters] */
import java.util.*;
public class Program38
{
    public static void add(double n1, double n2)
    {
        System.out.println(n1 + " + " + n2 + " = " + (n1+n2));
    }
    public static void subtract(double n1, double n2)
    {
        System.out.println(n1 + " - " + n2 + " = " + (n1-n2));
    }
    public static void multiply(double n1, double n2)
    {
        System.out.println(n1 + " * " + n2 + " = " + (n1*n2));
    }
    public static void divide(double n1, double n2)
    {
        System.out.println(n1 + " / " + n2 + " = " + (n1+n2));
    }
    public static void main()
    {
        Scanner key=new Scanner(System.in);
        System.out.println("Enter + to add 2 numbers, - to subtract 2 numbers, * to multiply 2 numbers and / to
divide 2 numbers:");
        char choice=key.next().charAt(0);
        System.out.println("Enter the first number:");
        double n1=key.nextDouble();
        System.out.println("Enter the second number:");
        double n2=key.nextDouble();
        switch(choice)
        {
             case '+':
             {
                 add(n1,n2);
             }
             break;
             case '-':
             {
                   subtract(n1,n2);
              }
              break;
              case '*':
              {
                   multiply(n1,n2);
              }
              break;
              case '/':
              {
                   divide(n1,n2);
              }
              break;
              default:
              {
                   System.out.println("Error - Invalid Input");
              }
         }
    }
}
Variable Listing:
Program 39:
/* Write a Java Program to pass 2 integer values to a function such that the first number is lower than the
second. The integer values will be the range, and the function computes the sum of the values in the range.
[with return, with parameters] */
import java.util.*;
public class Program39
{
    public static int sumOfRange(int l, int u)
    {
         int sum=0;
         for(int i=l; i<=u; i++)
         {
              sum+=i;
         }
         return(sum);
    }
    public static void main()
    {
         Scanner key=new Scanner(System.in);
         System.out.println("Enter the first number:");
         int n1=key.nextInt();
         System.out.println("Enter the second number:");
         int n2=key.nextInt();
         if(n1>n2)
         {
                  System.out.println("Error - The first number should be less than the second number");
                  System.exit(0);
         }
         System.out.println("The sum of the numbers from " + n1 + " to " + n2 + " is: " + sumOfRange(n1,n2));
    }
}
Variable Listing: