Académique Documents
Professionnel Documents
Culture Documents
} System.out.println ("La suma de los digitos de " + aux + " es: " + sumDig); } } }
Dado un numero verificar: - Que tenga dos digitos - Verificar si sus digitos son pares - Promediar sus digitos Cdigo:
class JavaAritmetica4 { public static void main (String args []) { int numero; System.out.print ("Inserte un numero de dos digitos pares: "); numero = Leer.datoInt (); int aux = numero; if (numero < 100 && numero > 9) { int d1 = numero % 10; numero = numero / 10; int d2 = numero % 10; if (d1 % 2 == 0 && d2 % 2 == 0) System.out.println ("El promedio de los digitos de: " + aux + " es: " + ((d1 + d2) / 2)); } } }
System.out.println ("\nVECTOR ORIGINAL: "); h.mostrar (V, d); System.out.println ("\nVECTOR LUEGO DE LA INVERSION: "); h.invierte (V, d); h.mostrar (V, d); } }
public static void main (String args []) { JavaVectores2 h = new JavaVectores2 (); int V [] = new int [20]; System.out.print ("Inserte dimen. del vector: "); int d = Leer.datoInt ();
System.out.print ("Inserte valor de (x): "); int x = Leer.datoInt (); h.llenar (V, d); System.out.println ("\nVECTOR: "); h.mostrar (V, d); h.evalua (V, d, x); } }
int max = M [1] [1]; for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { if (M [i] [j] < max) max = M [i] [j]; } } return (max); }
void intercambiar (int A [] [], int fa, int ca, int B [] [], int fb, int cb) { int min_a = menor (A, fa, ca); int max_b = maximo (B, fb, cb); //para cambiar los minimos de A con los maximos de B for (int i = 1 ; i <= fa ; i++) { for (int j = 1 ; j <= ca ; j++) { if (A [i] [j] == min_a) A [i] [j] = max_b; } } //para intercambiar los maximos de con los minimos de A for (int i = 1 ; i <= fb ; i++) { for (int j = 1 ; j <= cb ; j++) { if (B [i] [j] == max_b) B [i] [j] = min_a; } } } public static void main (String args []) { JavaMatrices1 h = new JavaMatrices1 (); int A [] [] = new int [20] [20]; int B [] [] = new int [20] [20]; System.out.print ("Insert filas de A: "); int fa = Leer.datoInt (); System.out.print ("Insert columnas de A: "); int ca = Leer.datoInt (); System.out.print ("Insert filas de B: "); int fb = Leer.datoInt (); System.out.print ("Insert columnas de B: "); int cb = Leer.datoInt (); //lectura de matrices System.out.println ("\nINSERTANDO DATOS EN MATRIS A: \n"); h.llenar (A, fa, ca); System.out.println ("\nINSERTANDO DATOS EN MATRIS B: \n");
h.llenar (B, fb, cb); System.out.println ("\nMATRICES ORIGINALMENTE INSERTADAS: "); h.mostrar (A, fa, ca); System.out.println (); h.mostrar (B, fb, cb); System.out.println (); //intercambiando elementos h.intercambiar (A, fa, ca, B, fb, cb); System.out.println ("\nMATRICES DESPUES DEL INTERCAMBIO:"); h.mostrar (A, fa, ca); System.out.println (); h.mostrar (B, fb, cb); } }
public static void main (String args []) { JavaMatrices2 h = new JavaMatrices2 (); int M [] [] = new int [20] [20]; System.out.print ("Inserte dimen. de la matris cuadrada: "); int d = Leer.datoInt (); h.llenar (M, d); System.out.print ("\nMATRIS ORIGINAL: "); h.mostrar (M, d); System.out.print ("\n\nMATRIS CON LA DIAGONAL PRINCIPAL INVERTIDA: "); h.invierte (M, d); h.mostrar (M, d); } }
public static void main (String args []) { JavaMatrices3 h = new JavaMatrices3 (); int M [] [] = new int [20] [20]; System.out.print ("Inserte dimen. de la matris cuadrada: "); int d = Leer.datoInt (); h.llenar (M, d); System.out.print ("\nMATRIS ORIGINAL: "); h.mostrar (M, d); System.out.print ("\n\nMATRIS CON LA DIAGONAL SECUNDARIA INVERTIDA: "); h.invierte (M, d); h.mostrar (M, d); } }
/*Dada dos matrices de diferentes tamanios R y S mostrar los elementos comunes de R en S*/ Cdigo:
class JavaMatrices4 { void llenar (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { System.out.print ("Inserte pos[" + i + "][" + j + "]: "); M [i] [j] = Leer.datoInt (); } } } void mostrar (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { System.out.println (); for (int j = 1 ; j <= c ; j++) { System.out.print ("[" + M [i] [j] + "]"); } } } void comunes (int R [] [], int fr, int cr, int S [] [], int fs, int cs) { System.out.print ("\n\nLos elementos comunes de R en S son: "); for (int i = 1 ; i <= fr ; i++) { for (int j = 1 ; j <= cr ; j++) {
for (int k = 1 ; k <= fs ; k++) { for (int l = 1 ; l <= cs ; l++) { if (R [i] [j] == S [k] [l]) System.out.print ("[" + R [i] [j] + "]"); } } } } }
public static void main (String args []) { JavaMatrices4 h = new JavaMatrices4 (); int R [] [] = new int [20] [20]; int S [] [] = new int [20] [20]; System.out.print ("Inserte filas de R: "); int fr = Leer.datoInt (); System.out.print ("Inserte columnas de R: "); int cr = Leer.datoInt (); System.out.print ("Inserte filas de S: "); int fs = Leer.datoInt (); System.out.print ("Inserte columnas de S: "); int cs = Leer.datoInt (); System.out.print ("\nLLENANDO MATRIS h.llenar (R, fr, cr); System.out.print ("\nLLENANDO MATRIS h.llenar (S, fs, cs); System.out.print ("\nLA MATRICES R : h.mostrar (R, fr, cr); System.out.print ("\nLA MATRICES S : h.mostrar (S, fs, cs); h.comunes (R, fr, cr, S, fs, cs); } } R: \n"); S: \n"); "); ");
/*Dada una matris intercambiar los elementos de la primera columna con la ultima columna*/ Cdigo:
class JavaMatrices5 { void llenar (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { System.out.print ("Inserte pos[" + i + "][" + j + "]: "); M [i] [j] = Leer.datoInt (); } } }
void mostrar (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { System.out.println (); for (int j = 1 ; j <= c ; j++) { System.out.print ("[" + M [i] [j] + "]"); } } } void intercambiar (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { int aux = M [i] [1]; M [i] [1] = M [i] [c]; M [i] [c] = aux; } } public static void main (String args []) { JavaMatrices5 h = new JavaMatrices5 (); int M [] [] = new int [20] [20]; System.out.print ("Inserte filas de la matris: "); int f = Leer.datoInt (); System.out.print ("Inserte columnas de la matris: "); int c = Leer.datoInt (); System.out.print ("\nLLENANDO MATRIS : \n"); h.llenar (M, f, c); System.out.print ("\nLA MATRIS ORIGINAL : "); h.mostrar (M, f, c); System.out.print ("\n\nLA MATRICES INTERCAMBIADA : "); h.intercambiar (M, f, c); h.mostrar (M, f, c); } }
void mostrar (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { System.out.println (); for (int j = 1 ; j <= c ; j++) { System.out.print ("[" + M [i] [j] + "]"); } } } void cuenta (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { System.out.print ("\n[" + M [i] [j] + "] tiene: " + digitos (M [i] [j]) + " digito(s)"); } } } int digitos (int n) { int contador = 0; while (n != 0) { n = n / 10; contador++; } return (contador); } public static void main (String args []) { JavaMatrices6 h = new JavaMatrices6 (); int M [] [] = new int [20] [20]; System.out.print ("Inserte filas de la matris: "); int f = Leer.datoInt (); System.out.print ("Inserte columnas de la matris: "); int c = Leer.datoInt (); System.out.print ("\nLLENANDO MATRIS M: \n"); h.llenar (M, f, c); System.out.print ("\nLA MATRIS: "); h.mostrar (M, f, c); System.out.print ("\n\nCONTEO DE DIGITOS: "); h.cuenta (M, f, c); } }
{ for (int k = 1 ; k <= d ; k++) { if (M [j] [i] != V [k]) sw = 0; } } if (sw == 1) System.out.println ("\n\nLa columna " + i + " es igual al vector"); } }
public static void main (String args []) { JavaMatrisVector2 h = new JavaMatrisVector2 (); int M [] [] = new int [20] [20]; int V [] = new int [20]; System.out.print ("Inserte filas de la matris: "); int f = Leer.datoInt (); System.out.print ("Inserte dimension del vector: "); int d = Leer.datoInt (); System.out.print ("\nLLENANDO MATRIS: \n"); h.llenarMatris (M, f, d); System.out.print ("\nLLENANDO EL VECTOR: \n"); h.llenarVector (V, d); System.out.print ("\nLA MATRIS: "); h.mostrarMatris (M, f, d); System.out.print ("\n\nEL VECTOR: \n"); h.mostrarVector (V, d); h.procedure (M, f, d, V, d); } }
/*Dada una matris Z almacenar en un vector A la suma por sus columnas y en un vector B la suma por sus filas*/ Cdigo:
class JavaMatrisVector3 { void llenarMatris (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { System.out.print ("Inserte pos[" + i + "][" + j + "]: "); M [i] [j] = Leer.datoInt (); } } } void mostrarMatris (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { System.out.println (); for (int j = 1 ; j <= c ; j++) { System.out.print ("[" + M [i] [j] + "]"); } } } void mostrarVector (int V [], int d) { for (int i = 1 ; i <= d ; i++) { System.out.print ("[" + V [i] + "]"); } } void vectorA (int M [] [], int f, int c, int A [], int d) { for (int i = 1 ; i <= f ; i++) { int suma = 0; for (int j = 1 ; j <= c ; j++) { suma = suma + M [j] [i]; } A [i] = suma; } } void vectorB (int M [] [], int f, int c, int B [], int d) { for (int i = 1 ; i <= f ; i++) { int suma = 0;
for (int j = 1 ; j <= c ; j++) { suma = suma + M [i] [j]; } B [i] = suma; } }
public static void main (String args []) { JavaMatrisVector3 h = new JavaMatrisVector3 (); int Z [] [] = new int [20] [20]; int A [] = new int [20]; int B [] = new int [20]; System.out.print ("Inserte filas de la matris: "); int f = Leer.datoInt (); System.out.print ("Inserte columnas de la matris: "); int c = Leer.datoInt (); System.out.print ("\nLLENANDO MATRIS: \n"); h.llenarMatris (Z, f, c); System.out.print ("\nLA MATRIZ Z: "); h.mostrarMatris (Z, f, c); System.out.println ("\n\nSUMA POR COLUMNAS DE LA MATRIS (vector A): "); h.vectorA (Z, f, c, A, c); h.mostrarVector (A, c); System.out.println ("\n\nSUMA POR FILAS DE LA MATRIS (vector B): "); h.vectorB (Z, f, c, B, f); h.mostrarVector (B, f); } }