ejemplos java.docx
TRANSCRIPT
![Page 1: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/1.jpg)
Reportar números del 1 al 10.
Resolvemos el problema usando while12345678910
public static void main (String args []){
int n=0;while(n<10){
n++;System.out.print(n+"n");
}}
Resolvemos el problema usando do … while123456789
public static void main (String args []){
int n=0;do{
n++;System.out.print(n+" ");
}while(n<10);}
Resolvemos el problema usando for123456789
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 8012345678
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 501234
public class Ejemplo{
public static void main(String[] args){
![Page 2: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/2.jpg)
56789
for(int i=100;i>=50;i=i-2)System.out.print(i+" ");
}}
Ingresar N números enteros y reportar la cantidad de pares y la cantidad de
impares.123456789101112131415161718192021222324
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++;elseci++;
}System.out.println("La cantidad de pares son: " + cp);System.out.println("La cantidad de impares son: " + ci);
}}
Ingresar N números y reportar la cantidad de positivos, negativos y ceros.123456789101112131
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++;
![Page 3: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/3.jpg)
41516171819202122232425262728293031
elsecn++;
}}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.1234567891011
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");
![Page 4: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/4.jpg)
12131415161718192021222324252627282930313233
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 es : " + pv + "%");System.out.println("El porcentaje de mujeres es : " + pm + "%");
}}
Calcular el siguiente producto:
P = 1*2*3*4*5*6*…*501234
public class Ejemplo{
public static void main(String[] args){
![Page 5: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/5.jpg)
567891011
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 números enteros del 1 al 100.1234567891011
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 números naturales.12345678910
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 números
}}
Se desea calcular independientemente la suma de los pares e impares
comprendidos entre 1 y 50.12345678910111
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);
}}
![Page 6: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/6.jpg)
2Se desea calcular independientemente la suma de los impares y el producto
de todos los impares comprendidos entre 20 y 8012345678910111213141516
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 número entero positivo y reportar su tabla de multiplicar.12345678910111213
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 número entero mayor o igual que cero.12345
import java.util.Scanner;
public class Ejemplo{public static void main(String[] args){
![Page 7: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/7.jpg)
678910111213141516171819
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 números. Se pide calcular el promedio de ellos1234567891011121314151617
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);
}}
![Page 8: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/8.jpg)
181920212223
Ingresar n números enteros, visualizar la suma de los números pares de la
lista, cuantos pares existen y cuál es la media de los números impares.123456789101112131415161718192021222
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){
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");
![Page 9: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/9.jpg)
3242526272829303132333435363738394041424344
}}
Ingresar n números y reportar el promedio de los positivos y el promedio de
los negativos.12345
import java.util.Scanner;
public class Ejemplo{
public static void main(String[] args)
![Page 10: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/10.jpg)
67891011121314151617181920212223242526272829303132
{
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;}elseif(x<0){
cn++;sn=sn+x;
}}if(cp>0){
pp=(double)sp/cp;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");}}
![Page 11: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/11.jpg)
333435363738394041424344454647
Ingresar n números, Calcular el mayor y el menor de ellos.1234567891011121314
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;
}
![Page 12: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/12.jpg)
15161718192021222324252627
System.out.println("El maximo es : "+maximo);System.out.println("El minimo es : "+minimo);
}}
Calcular la suma de los “n” términos de la serie:
S=1 – 1/2 + 1/3 – 1/4 + 1/5 – 1/6 + … 1/n1234567891011121314151
import java.util.Scanner;
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);
}}
![Page 13: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/13.jpg)
617181920212223
Realizar un programa que escriba los “n” términos de la serie de Fibonacci
1, 1, 2, 3, 5, 8, 13, 21, …123456789101112131415161718192021
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();
}}
![Page 14: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/14.jpg)
2223
Leer números hasta que el último número ingresado sea -99 (este no se
toma en cuenta para el cálculo) y reportar el mayor.12345678910111213141516171819202122232425
import java.util.Scanner;
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 positivo1234567891011121
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;
![Page 15: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/15.jpg)
31415161718192021222324
s=s+p/f;}System.out.println("La sumatoria es : "+s);
}}
Programa para ingresar un número entero positivo y reportar todos sus
divisores.1234567891011121314151617
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();}}
![Page 16: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/16.jpg)
1819
Ingresar un número entero y reportar si es primo. Un número es primo
cuando es divisible por si mismo y la unidad.1234567891011121314151617181920212223242526
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");}
}
![Page 17: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/17.jpg)
Ingresar un número entero positivo y reportar si es perfecto. Un número es
perfecto si es igual a la suma de divisores menores que él. Por ejemplo 6 es
perfecto pues es igual 1 + 2 + 3.12345678910111213141516171819202122
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 número y reportar todos sus factores primos. Por ejemplo si
ingresamos 12 debe reportar 2x2x3. Si ingresamos 25 se debe reportar 5×5.1234567891
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();
![Page 18: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/18.jpg)
011121314151617181920212223
}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("");}}
Ingresar 2 números enteros positivos y reportar su máximo común divisor.1234567891011121314151
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
![Page 19: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/19.jpg)
6171819202122232425262728293031
d++;System.out.println("El m.c.d de "+x+" y "+y+" es : "+p);
}}
Ingresar un número entero positivo y reportar su suma de dígitos.123456789101112131
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;num=num/10;
}System.out.println("La suma de sus digitos es : "+sum);
}
![Page 20: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/20.jpg)
4151617181920212223
}
Ingresar un numero entero positivo y reportar si es capicúa123456789101112131415161718192
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!!");}}
![Page 21: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/21.jpg)
0212223
Ingresar un numero entero en base 10 y reportar el numero en base b
( entre 2 y 9)123456789101112131415161718192021222324252627
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){
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 números impares
comprendidos entre 20 y 80.
Leer n números enteros y obtener el promedio de los positivos y el promedio
de los negativos.
Calcular la suma de los cuadrados de los números desde el 1 hasta el 15.
Se ingresan n números. Se pide calcular el promedio de ellos
Ingresar n números enteros, visualizar la suma de los números pares de la
lista, cuantos pares existen y cuál es la media de los números impares.
Desarrolle un programa que determine en un conjunto de números
naturales.
![Page 22: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/22.jpg)
Cuantos son menores de 15
Cuantos son mayores de 50
Cuantos están 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 números, Calcular el máximo y el mínimo de ellos.
Realizar un programa que escriba los n términos de la serie de Fibonacci
1, 1, 2, 3, 5, 8, 13, 21, …
Leer Números (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 MÉTODOS EN JAVADadas dos matrices A y B intercambiar los minimos de A con los maximos de
B1234567891011121314151617181920212223242526272829
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);
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++)
![Page 23: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/23.jpg)
3031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
{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 [] [], int{int min_a = menor (A, fa, ca);int max_b = maximo (B, fb, cb);
//para cambiar los minimos de A con los maximos de Bfor (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 Afor (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 matricesSystem.out.println ("nINSERTANDO DATOS EN MATRIZ A: n");
![Page 24: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/24.jpg)
8081828384858687888990919293949596979899100101102103104105106107108109110111112113
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 elementosintercambiar (A, fa, ca, B, fb, cb);System.out.println ("nMATRICES DESPUES DEL INTERCAMBIO:");mostrar (A, fa, ca);System.out.println ();mostrar (B, fb, cb);}}
Dada una matriz cuadrada invertir su diagonal principal123456789101112131415
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 pos["M [i] [j] = Leer.nextInt();}
}}public static void mostrar (int M [] [], int d){
![Page 25: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/25.jpg)
1617181920212223242526272829303132333435363738394041424344454647484950515253
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);}
}
Dada una matriz cuadrada invertir su diagonal secundaria12345678910
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();
![Page 26: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/26.jpg)
11121314151617181920212223242526272829303132333435
}}}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);System.out.print ("nMATRIZ ORIGINAL: ");mostrar (M, d);System.out.print ("nnMATRIZ CON LA DIAGONAL SECUNDARIA INVERTIDA: ");invierte (M, d);mostrar (M, d);}
}
![Page 27: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/27.jpg)
363738394041424344454647484950515253
Dada dos matrices de diferentes tamaños R y S mostrar los elementos
comunes de R en S123456789101112
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();}
![Page 28: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/28.jpg)
1314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
}}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, int{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])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 R: n");llenar (R, fr, cr);System.out.print ("nLLENANDO MATRIZ S: n");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);}}
![Page 29: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/29.jpg)
6364656667686970
Dada una matriz intercambiar los elementos de la primera columna con la
ultima columna1234567891011121314151617181920212223242
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 + "]: ");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 : ");
![Page 30: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/30.jpg)
52627282930313233343536373839404142434445464748495
mostrar (M, f, c);System.out.print ("nnLA MATRICES INTERCAMBIADA : ");intercambiar (M, f, c);mostrar (M, f, c);}}
![Page 31: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/31.jpg)
051525354
Contar el numero de digitos de cada elemento de una matriz12345678910111213141516171819202122232425262728293031323334353637383940
import java.util.Scanner;
public class JavaMatrizMetod6 {
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 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)");}
}}public static int digitos (int n){int contador = 0;while (n != 0){
n = n / 10;contador++;
}return (contador);}
![Page 32: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/32.jpg)
41424344454647484950515253545556575859606162636465
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);}}
Dada la matriz de m*n y el vector de tamaño n, determinar que columna de
la matriz es igual al vector1234567891011121314151617181920212223
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)
![Page 33: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/33.jpg)
2425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
{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++){
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);}}
![Page 34: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/34.jpg)
74757677787980818283
Dada una matriz Z almacenar en un vector A la suma por sus columnas y en
un vector B la suma por sus filas1234567891011121314151617181920212223242526272829303132333435363738
import java.util.Scanner;
public class JavaMatrizVectorMetod2 {
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;
}}
![Page 35: ejemplos java.docx](https://reader033.vdocuments.co/reader033/viewer/2022061614/55cf9bec550346d033a7df19/html5/thumbnails/35.jpg)
394041424344454647484950515253545556575859606162636465666781
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];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);}}