Sei sulla pagina 1di 27

Reportar nmeros del 1 al 10.

Resolvemos el problema usando while

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

public static void main (String args []) { int n=0; while(n<10) { n++; System.out.print(n+"n"); } }

Resolvemos el problema usando do while


public static void main (String args []) { int n=0; do{ n++; System.out.print(n+" "); }while(n<10); }

Resolvemos el problema usando for


public static void main (String args []) { int n=0; for(int i=1;i<=10;i++) { System.out.print(i+" "); } }

Reportar los siguiente serie: 20 25 30 35 70 75 80

1 2 3 4 5 6 7 8

public class Ejemplo { public static void main(String[] args) { for (int i = 20; i < 80; i = i + 5) System.out.print(i + " "); } }

Reportar la siguiente serie: 100 98 96 94 56 54 52 50

1 2 3 4 5

public class Ejemplo { public static void main(String[] args) { for(int i=100;i>=50;i=i-2)

6 7 8 9

System.out.print(i+" "); } }

Ingresar N nmeros enteros y reportar la cantidad de pares y la cantidad de impares.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n, x, cp = 0, ci = 0; System.out.print("Ingrese la cantidad de numeros a revisar: "); n = scan.nextInt(); for (int i = 1; i <= n; i++) { System.out.print(i + ") Ingrese un numero: "); x = scan.nextInt(); if (x % 2 == 0) cp++; else ci++; } System.out.println("La cantidad de pares son: " + cp); System.out.println("La cantidad de impares son: " + ci); } }

Ingresar N nmeros y reportar la cantidad de positivos, negativos y ceros.


import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n, x, cp = 0, cn = 0, c=0; System.out.print("Ingrese la cantidad de numeros a revisar: "); n = scan.nextInt(); for (int i = 1; i <= n; i++) { System.out.print(i + ") Ingrese un numero: "); x = scan.nextInt(); if (x==0) c++; else { if(x>0) cp++; else cn++;

20 21 22 23 24 25 26 27 28 29 30 31

} } System.out.println("La cantidad de positivos son: " + cp); System.out.println("La cantidad de negativos son: " + cn); System.out.println("La cantidad de ceros son: " + c); } }

Ingresar el sexo de n personas y reportar el porcentaje de hombres y el porcentaje de mujeres.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n, x, cv = 0, cm = 0; double pv,pm; System.out.print("Ingrese numero de personas n = scan.nextInt(); System.out.print("Ingrese sexo n" + "[1] Varon n" + "[2] Mujer n"); for (int i = 1; i <= n; i++) { System.out.print("Persona " + i + " : x = scan.nextInt(); if (x==1) cv++; else { if(x==2) cm++; } } pv=(cv*100)/n; pm=(cm*100)/n; System.out.println("El porcentaje de varones System.out.println("El porcentaje de mujeres } }

: ");

");

es : " + pv + "%"); es : " + pm + "%");

Calcular el siguiente producto: P = 1*2*3*4*5*6**50

public class Ejemplo

2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10

{ public static void main(String[] args) { double prodt=1; for(int i=1;i<=50;i++) prodt=prodt*i; System.out.println("El producto es : " + prodt); } }

Calcular la sumatoria de los nmeros enteros del 1 al 100.


public class Ejemplo { public static void main(String[] args) { int sum=0; for(int i=1;i<=100;i++) sum=sum+i; System.out.println("La suma es : "+sum); } }

Calcular la suma de los cuadrados de los 15 primeros nmeros naturales.

public class Ejemplo { public static void main(String[] args) { int i,sc=0; for(i=1;i<=15;i++) sc=sc+i*i; System.out.println("La suma de los cuadrados de los primeros 15 nme } }

Se desea calcular independientemente la suma de los pares e impares comprendidos entre 1 y 50.

1 2 3 4 5 6 7 8 9 10 11 12

public class Ejemplo { public static void main(String[] args) { int i,sp=0,si=0; for(i=1;i<=50;i++) if(i%2==0) sp=sp+i; else si=si+i; System.out.println("La suma de pares es : "+sp); System.out.println("La suma de impares es : "+si); } }

Se desea calcular independientemente la suma de los impares y el producto de todos los impares comprendidos entre 20 y 80

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

public class Ejemplo { public static void main(String[] args) { int i,si=0; double pi=1; for(i=20;i<=80;i++) if(i%2!=0) { si=si+i; pi=pi*i; } System.out.println("La suma es : "+si); System.out.println("El producto es : "+pi); } }

Ingresar un nmero entero positivo y reportar su tabla de multiplicar.


public class Ejemplo { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n; System.out.print("Ingresa un numero entero positivo : "); n=sc.nextInt(); System.out.println("Tabla de multiplicar del "+n); for(int i=1;i<=12;i++) System.out.println(n+"x"+i+"="+n*i); } }

Calcular el factorial de un nmero entero mayor o igual que cero.


import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n,i; double f=1; do{ System.out.print("Ingrese numero positivo o cero : "); n=in.nextInt(); }while(n<0); for(i=1;i<=n;i++) f=f*i; System.out.println("El factorial es : "+f); } }

Ingresar n nmeros. Se pide calcular el promedio de ellos

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n,i; double x,s=0,p; do{ System.out.print("Valor de n : "); n=in.nextInt(); }while(n<=0); for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextDouble(); s=s+x; } p=s/n; System.out.println("El Promedio es : "+p); } }

Ingresar n nmeros enteros, visualizar la suma de los nmeros pares de la lista, cuantos pares existen y cul es la media de los nmeros impares.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner in =new Scanner(System.in); int n,i,x,sp=0,si=0,cp=0,ci=0; double mi; do{ System.out.print("Valor de n : "); n=in.nextInt(); }while(n<=0); for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextInt(); if(x%2==0) { cp++; sp=sp+x; } else { ci++; si=si+x; } } if(cp>0)

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

{ System.out.println("La suma de los numeros pares es : "+sp); System.out.println("La cantidad de numeros pares es : "+cp); } else System.out.println("No se Ingresaron numeros pares"); if(ci>0) { mi=(double)si/ci; System.out.println("La media de los impares es : "+mi); } else System.out.println("No se Ingresaron numeros impares"); } }

Ingresar n nmeros y reportar el promedio de los positivos y el promedio de los negativos.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n,i,x,sp=0,sn=0,cp=0,cn=0; double pp,pn; do{ System.out.print("Valor de n : "); n=in.nextInt(); }while(n<=0); for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextInt(); if(x>0) { cp++; sp=sp+x; } else if(x<0) { cn++; sn=sn+x; } } if(cp>0) { pp=(double)sp/cp;

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

System.out.println("El Promedio de positivos es : "+pp); } else System.out.println("No se Ingresaron Positivos"); if(cn>0) { pn=(double)sn/cn; System.out.println("El Promedio de Negativos es : "+pn); } else System.out.println("No se Ingresaron Negativos"); } }

Ingresar n nmeros, Calcular el mayor y el menor de ellos.

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { int n,i; double x,maximo,minimo; Scanner in = new Scanner(System.in); do{ System.out.print("Valor de n : "); n=in.nextInt(); }while(n<=0); maximo=-1e30; minimo=1e30; for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextDouble(); if(x>maximo) maximo=x; if(x<minimo) minimo=x; } System.out.println("El maximo es : "+maximo); System.out.println("El minimo es : "+minimo); } }

Calcular la suma de los n trminos de la serie: S=1 1/2 + 1/3 1/4 + 1/5 1/6 + 1/n

import java.util.Scanner;

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

public class Ejemplo { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n,i; double s=0; do{ System.out.print("Valor de n : "); n=sc.nextInt(); }while(n<=0); for(i=1;i<=n;i++) { if(i%2==0) s=s-1.0/i; else s=s+1.0/i; } System.out.println("La sumatoria es : "+s); } }

Realizar un programa que escriba los n trminos de la serie de Fibonacci 1, 1, 2, 3, 5, 8, 13, 21,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n,i; double p=1,s=0,t; do{ System.out.print("Numero de terminos : "); n=in.nextInt(); }while(n<=2); for(i=1;i<=n;i++) { t=p+s; System.out.print(t+" "); p=s; s=t; } System.out.println(); } }

Leer nmeros hasta que el ltimo nmero ingresado sea -99 (este no se toma en cuenta para el clculo) y reportar el mayor.

import java.util.Scanner;

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

public class Ejemplo { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n,i=0; double x,mayor; mayor=-1e30; do{ System.out.print("Ingrese numero (-99 para finalizar) : "); x=sc.nextDouble(); if(x!=-99) { i++; if(x>mayor) mayor=x; } }while(x!=-99); if(i>0) System.out.println("El mayor es : "+mayor); else System.out.println("No se ingresaron numeros"); } }

Calcular la sumatoria: S= 1 + x + x^2/2! + x^3/3! + x^4/4! + + x^n/n! Se debe ingresar x real y n entero positivo

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n,i; double p=1,x,f=1,s=1; System.out.print("Ingrese valor de x : "); x=sc.nextDouble(); do{ System.out.print("Valor de n : "); n=sc.nextInt(); }while(n<0); for(i=1;i<=n;i++) { f=f*i; p=p*x; s=s+p/f; } System.out.println("La sumatoria es : "+s); } }

24
Programa para ingresar un nmero entero positivo y reportar todos sus divisores.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

import java.util.Scanner; public class Ejemplo { public static void main(String args[]) { Scanner in = new Scanner(System.in); int num; do{ System.out.print("Ingrese numero :"); num=in.nextInt(); }while(num<=0); int d; System.out.println("Los divisores del numero son :"); for(d=1;d<=num;d++) if(num%d==0) System.out.print(d+" "); System.out.println(); } }

Ingresar un nmero entero y reportar si es primo. Un nmero es primo cuando es divisible por si mismo y la unidad.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

import java.util.Scanner; public class Ejemplo { public static void main(String args[]) { Scanner in = new Scanner(System.in); int num; do{ System.out.print("Ingrese numero :"); num=in.nextInt(); }while(num<=0); int n; int d=1; do{ d=d+1; }while( num%d!=0 && d*d<=num); if(d*d>num) n=1; else n=0; if(n==1) System.out.println("El numero es primo"); else System.out.println("El numero no es primo"); } }

Ingresar un nmero entero positivo y reportar si es perfecto. Un nmero es perfecto si es igual a la suma de divisores menores que l. Por ejemplo 6 es perfecto pues es igual 1 + 2 + 3.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num; do{ System.out.print("Ingrese numero :"); num=in.nextInt(); }while(num<=0); int d,sum=0; for(d=1;d<num;d++) if(num%d==0) sum=sum +d; if(sum==num) System.out.print("El numero es Perfecto!!"); else System.out.print("El numero NO es Perfecto!!"); System.out.println(); } }

Ingresar un nmero y reportar todos sus factores primos. Por ejemplo si ingresamos 12 debe reportar 2x2x3. Si ingresamos 25 se debe reportar 55.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

import java.util.Scanner; public class Ejemplo { public static void main(String args[]) { Scanner in = new Scanner(System.in); int num; do{ System.out.print("Ingrese numero :"); num=in.nextInt(); }while(num<=0); int d=2; System.out.print("Factores primos : "); while(num>1) { while(num % d !=0) d=d+1; System.out.print(d+" "); num=num/d; } System.out.println(""); } }

22 23
Ingresar 2 nmeros enteros positivos y reportar su mximo comn divisor.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

import java.util.Scanner; public class Ejemplo { public static void main(String args[]) { Scanner in=new Scanner(System.in); int x,y; do{ System.out.print("Ingrese primer numero :"); x=in.nextInt(); }while(x<0); do{ System.out.print("Ingrese el segundo numero : "); y=in.nextInt(); }while(y<0); int d=2,p=1,a,b; a=x; b=y; while(d<=a && d<=b) if(a%d==0 && b%d==0) { p=p*d; a=a/d; b=b/d; } else d++; System.out.println("El m.c.d de "+x+" y "+y+" es : "+p); } }

Ingresar un nmero entero positivo y reportar su suma de dgitos.


import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num, sum=0, digit; do { System.out.print("Ingrese un numero : "); num = sc.nextInt(); }while(num<0); while(num>0) { digit=num%10; sum=sum+digit;

16 17 18 19 20 21 22 23 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

num=num/10; } System.out.println("La suma de sus digitos es : "+sum); } }

Ingresar un numero entero positivo y reportar si es capica

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num, invert=0,aux, digit; System.out.print("Ingresa numero : "); num = sc.nextInt(); aux=num; while(aux>0) { digit=aux%10; invert=invert*10 + digit; aux=aux/10; } if(num==invert) System.out.println("El numero es Capicua!!"); else System.out.println("El numero NO es Capicua!!"); } }

Ingresar un numero entero en base 10 y reportar el numero en base b ( entre 2 y 9)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

import java.util.Scanner; public class Ejemplo { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int num,base; String result=" "; System.out.print("Ingrese el numero en base 10 : "); num= sc.nextInt(); do{ System.out.print("Ingrese la base : "); base= sc.nextInt(); }while(base>=9); if(num<base) result= num + result; while(num>=base) {

17 18 19 20 21 22 23 24 25 26 27

result= num%base + result; num=num/base; if(num<base) result= num + result; } System.out.println("El numero en base "+base+" es : "+result); } }

Se desea calcular independientemente la suma de los pares e impares comprendidos entre 1 y 50 (incluidos los extremos). Calcular y visualizar la suma y el producto de los nmeros impares comprendidos entre 20 y 80. Leer n nmeros enteros y obtener el promedio de los positivos y el promedio de los negativos. Calcular la suma de los cuadrados de los nmeros desde el 1 hasta el 15. Se ingresan n nmeros. Se pide calcular el promedio de ellos Ingresar n nmeros enteros, visualizar la suma de los nmeros pares de la lista, cuantos pares existen y cul es la media de los nmeros impares. Desarrolle un programa que determine en un conjunto de nmeros naturales. Cuantos son menores de 15 Cuantos son mayores de 50 Cuantos estn comprendidos entre 25 y 45. Calcular el factorial de un numero n>=0 Ingresar un valor de x y un valor n positivo reportar la potencia de x elevado a la n. Imprimir las 10 primeras potencias de 4. Ingresar n nmeros, Calcular el mximo y el mnimo de ellos. Realizar un programa que escriba los n trminos de la serie de Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, Leer Nmeros (el ultimo numero es -99) y obtener el mayor. Calcular la sumatoria s = 1 + x + x^2/2! + x^3/3! + x^4/4! + + x^n/n! Se debe ingresar x real y n entero positivo

EJERCICIOS RESUELTOS DE MATRICES CON MTODOS EN JAVA


Dadas dos matrices A y B intercambiar los minimos de A con los maximos de B

1 2 3 4 5

import <span class="adtext" id="adtext_5">java</span>.util.Scanner; <span class="adtext" id="adtext_1">public class</span> JavaMatrizMetod1 { Scanner Leer = new Scanner(System.in);

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

public static void llenar (int M [] [], int f, int c) { Scanner Leer = new Scanner(System.in); 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.nextInt(); } } } public static 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] + "]"); } } } public static int menor (int M [] [], int f, int c) { int men = M [1] [1]; for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { if (M [i] [j] < men) men = M [i] [j]; } } return (men); } public static int maximo (int M [] [], int f, int c) { 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); } public static void intercambiar (int A [] [], int fa, int ca, int B [] [], in { 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;

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

} } //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 []) { Scanner Leer = new Scanner(System.in); int A [] [] = new int [20] [20]; int B [] [] = new int [20] [20]; System.out.print ("Insert filas de A: "); int fa = Leer.nextInt(); System.out.print ("Insert columnas de A: "); int ca = Leer.nextInt(); System.out.print ("Insert filas de B: "); int fb = Leer.nextInt(); System.out.print ("Insert columnas de B: "); int cb = Leer.nextInt(); //lectura de matrices System.out.println ("nINSERTANDO DATOS EN MATRIZ A: n"); llenar (A, fa, ca); System.out.println ("nINSERTANDO DATOS EN MATRIZ B: n"); llenar (B, fb, cb); System.out.println ("nMATRICES ORIGINALMENTE INSERTADAS: "); mostrar (A, fa, ca); System.out.println (); mostrar (B, fb, cb); System.out.println (); //intercambiando elementos intercambiar (A, fa, ca, B, fb, cb); System.out.println ("nMATRICES DESPUES DEL INTERCAMBIO:"); mostrar (A, fa, ca); System.out.println (); mostrar (B, fb, cb); } }

106 107 108 109 110 111 112 113


Dada una matriz cuadrada invertir su diagonal principal

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

import java.util.Scanner; public class JavaMatrizMetod2 {

public static void llenar (int M [] [], int d) { Scanner Leer = new Scanner(System.in); for (int i = 1 ; i <= d ; i++) { for (int j = 1 ; j <= d ; j++) { System.out.<span class="adtext" id="adtext_3">print</span>("Inserte p M [i] [j] = Leer.nextInt(); } } } public static void mostrar (int M [] [], int d) { for (int i = 1 ; i <= d ; i++) { System.out.println (); for (int j = 1 ; j <= d ; j++) { System.out.print("[" + M [i] [j] + "]"); } } } public static void invierte (int M [] [], int d) { int fin = d; for (int i = 1 ; i <= d / 2 ; i++) { int aux = M [i] [i]; M [i] [i] = M [d] [d]; M [d] [d] = aux; fin--; } } public static void main (String args []) { Scanner Leer = new Scanner(System.in); int M [] [] = new int [20] [20]; System.out.print("Inserte dimen. de la matriz cuadrada: "); int d = Leer.nextInt(); llenar (M, d); System.out.print("nMATRIZ ORIGINAL: "); mostrar (M, d); System.out.print("nnMATRIZ CON LA DIAGONAL PRINCIPAL INVERTIDA: "); invierte (M, d); mostrar (M, d);

42 43 44 45 46 47 48 49 50 51 52 53 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

} }

Dada una matriz cuadrada invertir su diagonal secundaria


import java.util.Scanner; public class JavaMatrizMetod3 { public static void llenar (int M [] [], int d) { Scanner Leer = new Scanner(System.in); for (int i = 1 ; i <= d ; i++) { for (int j = 1 ; j <= d ; j++) { System.out.print ("Inserte pos[" + i + "][" + j + "]: "); M [i] [j] = Leer.nextInt(); } } } public static void mostrar (int M [] [], int d) { for (int i = 1 ; i <= d ; i++) { System.out.println (); for (int j = 1 ; j <= d ; j++) { System.out.print ("[" + M [i] [j] + "]"); } } } public static void invierte (int M [] [], int d) { int fin = d; for (int i = 1 ; i <= d / 2 ; i++) { int aux = M [i] [d]; M [i] [d] = M [d] [i]; M [d] [i] = aux; fin--; } } public static void main (String args []) { Scanner Leer = new Scanner(System.in); int M [] [] = new int [20] [20]; System.out.print ("Inserte dimen. de la matriz cuadrada: "); int d = Leer.nextInt(); llenar (M, d);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

System.out.print ("nMATRIZ ORIGINAL: "); mostrar (M, d); System.out.print ("nnMATRIZ CON LA DIAGONAL SECUNDARIA INVERTIDA: "); invierte (M, d); mostrar (M, d); } }

Dada dos matrices de diferentes tamaos R y S mostrar los elementos comunes de R en S

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

import java.util.Scanner; public class JavaMatrizMetod4 { public static void llenar (int M [] [], int f, int c) { Scanner Leer = new Scanner(System.in); 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.nextInt(); } } } public static 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] + "]"); } } } public static void comunes (int R [] [], int fr, int cr, int S [] [], int fs, { System.out.print ("nnLos 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])

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

System.out.print ("[" + R [i] [j] + "]"); } } } } } public static void main (String args []) { Scanner Leer = new Scanner(System.in); int R [] [] = new int [20] [20]; int S [] [] = new int [20] [20]; System.out.print ("Inserte filas de R: "); int fr = Leer.nextInt(); System.out.print ("Inserte columnas de R: "); int cr = Leer.nextInt(); System.out.print ("Inserte filas de S: "); int fs = Leer.nextInt(); System.out.print ("Inserte columnas de S: "); int cs = Leer.nextInt(); System.out.print ("nLLENANDO MATRIZ llenar (R, fr, cr); System.out.print ("nLLENANDO MATRIZ llenar (S, fs, cs); System.out.print ("nLA MATRICES R : mostrar (R, fr, cr); System.out.print ("nLA MATRICES S : mostrar (S, fs, cs); comunes (R, fr, cr, S, fs, cs); } } R: n"); S: n"); "); ");

Dada una matriz intercambiar los elementos de la primera columna con la ultima columna

1 2 3 4 5 6 7 8 9 10

import java.util.Scanner; public class JavaMatrizMetod5 { public static void llenar (int M [] [], int f, int c) { Scanner Leer = new Scanner(System.in); for (int i = 1 ; i <= f ; i++) { for (int j = 1 ; j <= c ; j++) { System.out.print ("Inserte pos[" + i + "][" + j + "]: ");

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 1 2 3 4 5

M [i] [j] = Leer.nextInt(); } } } public static 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] + "]"); } } } public static 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] 1; M [i] 1 = aux; } } public static void main (String args []) { Scanner Leer = new Scanner(System.in); int M [] [] = new int [20] [20]; System.out.print ("Inserte filas de la matriz: "); int f = Leer.nextInt(); System.out.print ("Inserte columnas de la matriz: "); int c = Leer.nextInt(); System.out.print ("nLLENANDO MATRIZ : n"); llenar (M, f, c); System.out.print ("nLA MATRIZ ORIGINAL : "); mostrar (M, f, c); System.out.print ("nnLA MATRICES INTERCAMBIADA : "); intercambiar (M, f, c); mostrar (M, f, c); } }

Contar el numero de digitos de cada elemento de una matriz


import java.util.Scanner; public class JavaMatrizMetod6 { public static void llenar (int M [] [], int f, int c) {

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

Scanner Leer = new Scanner(System.in); 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.nextInt(); } } } public static 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] + "]"); } }

} public static 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]) } } } public static int digitos (int n) { int contador = 0; while (n != 0) { n = n / 10; contador++; } return (contador); } public static void main (String args []) { Scanner Leer = new Scanner(System.in); int M [] [] = new int [20] [20]; System.out.print ("Inserte filas de la matriz: "); int f = Leer.nextInt(); System.out.print ("Inserte columnas de la matriz: "); int c = Leer.nextInt(); System.out.print ("nLLENANDO MATRIZ M: n"); llenar (M, f, c); System.out.print ("nLA MATRIZ: "); mostrar (M, f, c); System.out.print ("nnCONTEO DE DIGITOS: "); cuenta (M, f, c); } }

56 57 58 59 60 61 62 63 64 65
Dada la matriz de m*n y el vector de tamao n, determinar que columna de la matriz es igual al vector

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

import java.util.Scanner; public class JavaMatrizVectorMetod1 { public static void llenarMatriz (int M [] [], int f, int c) { Scanner Leer = new Scanner(System.in); 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.nextInt(); } } } public static void mostrarMatriz (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] + "]"); } } } public static void llenarVector (int V [], int d) { Scanner Leer = new Scanner(System.in); for (int i = 1 ; i <= d ; i++) { System.out.print ("Inserte pos.[" + i + "]: "); V [i] = Leer.nextInt(); } } public static void mostrarVector (int V [], int d) { for (int i = 1 ; i <= d ; i++) { System.out.print ("[" + V [i] + "]"); } } public static void procedure (int M [] [], int f, int c, int V [], int d) { for (int i = 1 ; i <= f ; i++)

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

{ int sw = 1; for (int j = 1 ; j <= c ; j++) { for (int k = 1 ; k <= d ; k++) { if (M [j] [i] != V [k]) sw = 0; } } if (sw == 1) System.out.println ("nnLa columna " + i + " es igual al vector"); } } public static void main (String args []) { Scanner Leer = new Scanner(System.in); int M [] [] = new int [20] [20]; int V [] = new int [20]; System.out.print ("Inserte filas de la matriz: "); int f = Leer.nextInt(); System.out.print ("Inserte dimension del vector: "); int d = Leer.nextInt(); System.out.print ("nLLENANDO MATRIZ: n"); llenarMatriz(M, f, d); System.out.print ("nLLENANDO EL VECTOR: n"); llenarVector (V, d); System.out.print ("nLA MATRIZ: "); mostrarMatriz(M, f, d); System.out.print ("nnEL VECTOR: n"); mostrarVector (V, d); procedure (M, f, d, V, d); } }

Dada una matriz Z almacenar en un vector A la suma por sus columnas y en un vector B la suma por sus filas

1 2 3

import java.util.Scanner; public class JavaMatrizVectorMetod2 {

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

public static void llenarMatriz (int M [] [], int f, int c) { Scanner Leer = new Scanner(System.in); 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.nextInt(); } } } public static void mostrarMatriz (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] + "]"); } } } public static void mostrarVector (int V [], int d) { for (int i = 1 ; i <= d ; i++) { System.out.print ("[" + V [i] + "]"); } } public static 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; } } public static 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 []) { Scanner Leer = new Scanner(System.in); int Z [] [] = new int [20] [20]; int A [] = new int [20]; int B [] = new int [20];

54 55 56 57 58 59 60 61 62 63 64 65 66 67 81

System.out.print("Inserte filas de la matriz: "); int f = Leer.nextInt(); System.out.print("Inserte columnas de la matriz: "); int c = Leer.nextInt(); System.out.print("nLLENANDO MATRIZ: n"); llenarMatriz(Z, f, c); System.out.print("nLA MATRIZ Z: "); mostrarMatriz(Z, f, c); System.out.println("nnSUMA POR COLUMNAS DE LA MATRIZ (vector A): "); vectorA(Z, f, c, A, c); mostrarVector (A, c); System.out.println("nnSUMA POR FILAS DE LA MATRIZ (vector B): "); vectorB(Z, f, c, B, f); mostrarVector (B, f); } }

Potrebbero piacerti anche