Java Saved Program: gifted-city

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
/* Name of the class has to be "Script" only */
class Script
{
	public static void main (String[] args)
	{
	    Scanner skener = new Scanner(System.in);
	    int m;
	    int maxNum;
	    int minNum;
	    
	    System.out.println("Unesite dimenzije kvadratne matrice: ");
	    do{
	        m = skener.nextInt();
	    }while(m<3 || m>9);
	    
	    int[][] polje = new int[m][m];
	    
	    System.out.println("Unesite vrijednosti elemenata u matricu");
	    for(int i=0; i<m; i++)
	    {
	        for(int j=0; j<m; j++)
	        {
	            System.out.println("Element " + (i+1) + ". stupca, " + (j+1) + ". retka");
	            polje[i][j] = skener.nextInt();
	        }
	    }
	    
	    for(int i=0; i<m; i++)
	    {
	        for(int j=0; j<m; j++)
	        {
	            System.out.print(polje[i][j] + "\t");
	        }
	        System.out.println();
	    }
	    
	    minNum = polje[0][0];
	    for(int i=1; i<m; i++)
	    {
	        if(polje[i][i] > minNum)
	        {
	            minNum = polje[i][i];
	        }
	    }
	    System.out.println("Najmanji broj na glavnoj dijagonali je: " + minNum);
	    
	    maxNum = polje[m-1][0];
	    for(int i=0; i<m; i++)
	    {
	        for(int j=m-1; j>=0; j--)
	        {
	        if((polje[i][j] > maxNum) && (i+j+1 == m))
	        {
	            maxNum = polje[i][j];
	        }
	        }
	    }
	     System.out.print("Najveci broj na sporednoj dijagonali je: " + maxNum);
	}
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs