Max element in row and min element in column in java

Write a java program to maximum element in given row and minimum element in given column.

package array;
  
import java.util.*;
public class maxmininrowcolumn 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int m,n,i,j,sum=0,ch,min=0;
        System.out.println("enter m::");
        m=s.nextInt();
        System.out.println("enter n::");
        n=s.nextInt();
        int a[][]=new int[m][n];
        System.out.println("enter elements..");
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                a[i][j]=s.nextInt();
            }
        }
        System.out.println("ur matrix is....");
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print("  "+a[i][j]);
            }
            System.out.println();
        }
    
        System.out.println("1 Row\n");
        System.out.println("2 Column\n");
        System.out.println("3 Exit\n");
        System.out.println("4 Choice?\n");
        System.out.println("enter your choice:");
        ch=s.nextInt();
      
        switch(ch)
        {
            case 1:
                int row;
                System.out.println("enter the row::");
                row=s.nextInt();
                int max=0;
                for(i=0;i<n;i++)
                {
                    if(max<a[row][i])
                    {
                        max=a[row][i];
                    }
                    //System.out.println("Max number is::"+max);
                }
                System.out.println("Max number is::"+max);
                break;
            case 2:
                    int col;
                    System.out.println("enter column");
                    col=s.nextInt();
                    min=a[0][col];
                    for(i=1;i<m;i++)
                    {
                        if(min>a[i][col])
                        {
                            min=a[i][col];
                        }
                      //  System.out.println("min number is::"+min);
                    }
                    System.out.println("Min number is::"+min);
                    break;
            case 3:
                System.out.println("Want a exit...");
                break;
            case 4:
                System.out.println("ur choice is not good...!");
                break;
            default:
                System.out.println("Wrong choice....!");
                break;
        }
        
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *