viernes, 8 de junio de 2018

EJERCICIO 3


1.    
Hacer un programa con matrices que muestre únicamente los valores de la diagonal por ejemplo si la matriz es de 3 x 3 solo deberá imprimir. [3 8 3]



CÓDIGO FUENTE


package manu;
import javax.swing.JOptionPane;
/**
 *         
 * @author Admin
 */
public class LeerArreglo{
 public static void main(String arg[])
    {    
    int v[] ={774, 647, 192, 754, 515, 578, 861, 947, 253};   
    int n= (int)Math.sqrt(v.length);
   
    int m[][] = new int[n][n];
    int cont = 0;
    for (int x = 0; x < n; x++)
        {
      for (int y = 0; y < n; y++)
            {
                m[y][x] = v[cont];
                cont++;
            }
        } 
        System.out.println("Matriz:");
    String str = " ";
    for (int i = 0; i < n; i++)
        {
      for (int j = 0; j < n; j++)
            {
                str += m[j][i]+" ";
            }
            System.out.println(str);
            str = " ";
        }
    System.out.print("La diagonal principal es:\n ");
        for (int i=0; i<n; i++)
            for (int j=0; j<n; j++){
                if (i == j){
                    System.out.print(m[i][j]+"\t");
                 } else if(i != j) {
                    System.out.print(" ");
                } 
            }
  }}






SALIDA



No hay comentarios.:

Publicar un comentario

VENTANAS DE DIALOGO

Que Es? JOptionPane es una Clase que nos provee una serie de ventanas de dialogo predefinidas con el fin de facilitarnos algunos proces...