Documentos de Académico
Documentos de Profesional
Documentos de Cultura
b.-Utilizando mtodos
Inicio Variables N, fac leenum ( ) Variables N Leer N N 0 retornar N factorial (N) Variables i, fac=1 i=1 ; i N; i=i+1 fac = fac * i retornar fac
N = leenum( ) fac = factorial(N ) reportar (N,fac) Fin Reportar ( N, fac) Variables Escribir N, fac retornar
2.-Leer dos nmeros enteros positivos X, N y calcular el valor de X por multiplicaciones sucesivas.
a.- Sin utilizar mtodos
import java.io.*; public class ejem_2 { public static void main(String []args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, X, pot=1, i; do{System.out.print("ingrese un numero :"); N = Integer.parseInt(br.readLine()); }while(N<=0); do{System.out.print("ingrese un numero : "); X = Integer.parseInt(br.readLine()); }while(X<=0);
for( i=1 ; i<=N ; i++) { pot = pot * X; } System.out.println("La potencia de "+X+" a la "+ N + es = " +pot); } }
import java.io.*; public class ejem_metodos2 { static BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); /*********mtodo principal*****/ public static void main(String []args) throws IOException { int X ,N, pot; X= leenum( ); N= leenum( ); pot = potencia(X , N); reportar(X, N, pot); } /*********ingreso se datos*****/ static int leenum( ) throws IOException { int Nu; do{System.out.print("ingrese un numero : "); Nu = Integer.parseInt(br.readLine()); }while(Nu<=0); return Nu; } /*******proceso de potencia*****/ static int potencia (int X, int N) throws IOException { int i , pot=1; for( i=1 ; i<=N ; i++) {pot = pot *X; } return pot ; } /******** reporte de resultados******/ static void reportar(int X, int N , int pot) {System.out.print("La potencia de "+X+" a la "+ N +"es ="+pot); return ; } 2 }
3.Leer un nmero entero positivo N , calcular e imprimir sus divisores y reportar cuanto son en total.
a.Primero sin usar mtodos import java.io.*; public class ejem_3 { public static void main(String []args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, i, cdiv=0; do { System.out.print("ingrese un numero :"); N = Integer.parseInt(br.readLine()); } while(N<=0); System.out.println("Los divisores son :"); for ( i =1 ; i<=N ; i++) { if (N%i == 0) { System.out.print("\t" + i ); cdiv ++; } } System.out.println("El Total de divisores de "+ N + " es = " + cdiv); } } Fin
N%i=0
Escribir : N, cdiv
import java.io.*; public class ejem_metodos3 {static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /******* mtodo principal ***********/ public static void main(String []args) throws IOException { int N, cdiv; N = leernum( ); cdiv = calcula_div( N); reportar( N, cdiv) } /*******Leer nmero***********/ static int leernum( ) throws IOException { int N; do { System.out.print("ingrese un numero :"); N = Integer.parseInt(br.readLine()); } while (n<=0); return N; } /*******************/ static int calcula_div ( int N ) throws IOException { int i, cdiv =0; System.out.println("Los divisores son "); i = i +1 F for (i=0 ; i<= N ; i++) { if (N%i==0) { System.out.print("\t" + i); cdiv ++; } } return cdiv; } /*******************/ static void reportar ( int N, int cdiv ) throws IOException { System.out.println("El Total de divisores de "+ N + " es = " + cdiv); return ; } } // llave de la clase
N = leernum( ) cdiv = calcula_div( N) reportar( N, cdiv) Fin leernum( ) Variables Leer N N 0 retornar N calcula_div ( N ) Variables i , cdiv =0 i = 1; V i N ; N%i=0 N
4.- Leer un nmero entero positivo N, calcular e imprimir sus divisores y reportar la suma de los divisores.
Inicio Variables N, sum
import java.io.*; public class ejem_metodos4 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N, sum; N= leenum(); sum = calcular(N); reportar(sum); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero :"); N = Integer.parseInt(br.readLine()); }while(N<=0); return N; } /*******proceso de calcular*****/ static int calcular (int N) throws IOException { int i , sum=0; System.out.println("Los divisores de " +N+ "son : "); for( i=1 ; i<=N ; i++) {if ( N% i == 0) { System.out.print ("\t" + i ); sum = sum + i; } } return sum ; } /******** reporte de resultados******/ static void reportar ( int sum) throws IOException {System.out.println("Suma de los divisores es = " +sum); return ; } }
5.- Leer dos nmeros enteros positivos N1 y N2, calcular el producto por sumas sucesivas y reportar el producto calculado.
Inicio Variables N1, N2, pro N1 = leernum( ) N2 = leernum( ) pro= calcular_pro( N1,N2 ) reportar ( N1,N2,pro ) Fin leernum( ) Variables Leer N N 0 retornar N N
InputStreamReader(System.in));
/********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N1, N2, pro; N1= leernum( ); N2= leernum( ); pro = calcular_pro(N1,N2); reportar(pro); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero :"); N = Integer.parseInt(br.readLine()); }while(N<=0); return N; }
/*******proceso de calcular*****/ static int calcular_pro (int N1, int N2) throws IOException { int i , pro=0; for( i=1 ; i<=N1 ; i++) { pro = pro + N2; } return pro ; } /******** reporte de resultados******/
static void reportar ( int N1,int N2, int pro) throws IOException {System.out.println("El producto de " +N1+"*"+N2+"es="+pro);
retornar pro
return ; } }
retornar
6.- Leer dos nmeros enteros positivos N1, N2, de dos dgitos cada uno, unirlos en un solo nmero N =N1N2 y reportar el nmero obtenido.
Inicio Variables N1, N2, N N1 = leernum( ) N2 = leernum( ) N= calcular_num( N1,N2 ) reportar ( N) Fin
import java.io.*; public class ejem_metodos6 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N1, N2, N; N1= leernum( ); N2= leernum( ); N = calcular_num(N1,N2); reportar(N); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N<10 || N>99); return N; }
/*******proceso de calcular*****/ static int calcular_num (int N1, int N2) throws IOException { int N; N = N1*100 + N2; return N ; }
/******** reporte de resultados******/ static void reportar ( int N) throws IOException {System.out.println("El Nmero es = " +N); return ; } }
7.- Leer dos nmeros enteros positivos N1, N2, de uno o dos dgitos cada uno, unirlos en un solo nmero N =N1N2 y reportar el nmero obtenido.
Inicio Variables N1, N2, N N1 = leernum( ) N2 = leernum( ) N= calcular_num( N1,N2 ) reportar ( N) Fin
import java.io.*; public class ejem_metodos7 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N1, N2, N; N1= leernum( ); N2= leernum( ); N = calcular_num(N1,N2); reportar(N); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N<1 || N>99); return N; }
leernum( ) Variables Leer N N <1 V N >99 retornar N calcular_num ( N1,N2) Variables N V N2 < 10 F N
N = N1 * 10 + N2
N = N1 * 100 + N2
retornar N
/*******proceso de calcular*****/ static int calcular_num (int N1, int N2) throws IOException { int N; if( N2 < 10) {N = N1*10 + N2; } else{ N = N1*100 + N2; } return N ; }
/******** reporte de resultados******/ static void reportar ( int N) throws IOException {System.out.println("El Nmero es = " +N); return ; } }
8.- Leer dos nmeros enteros positivos N1, N2, de dos dgitos cada uno, unirlos en un solo nmero N =N1N2 o N= N2N1, de tal modo que N tenga el mayor valor posible y reportar el nmero obtenido.
Inicio Variables N1, N2, N N1 = leernum( ) N2 = leernum( ) N= calcular_num( N1,N2 ) reportar ( N) Fin
import java.io.*; public class ejem_metodos8 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N1, N2, N; N1= leernum( ); N2= leernum( ); N = calcular_num(N1,N2); reportar(N); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N<10 || N>99); return N; }
N = N1*100 + N2
N = N2 * 100 + N1
retornar N
/*******proceso de calcular*****/ static int calcular_num (int N1, int N2) throws IOException { int N; if( N1 > N2) {N = N1*100 + N2; } else{ N = N2*100 + N1; } return N ; } /******** reporte de resultados******/ static void reportar ( int N) throws IOException { System.out.print("El Nmero es = " +N); return ; } }
9.- Leer dos nmeros enteros positivos N1, N2, de uno o dos dgitos cada uno, unirlos en un solo nmero N =N1N2 o N= N2N1, de tal modo que N tenga el mayor valor posible y reportar el nmero obtenido.
Inicio Variables N1, N2, N N1 = leernum( ) N2 = leernum( ) N= calcular_num( N1,N2 ) reportar ( N) Fin leernum( ) Variables Leer N N <1 V N >99 retornar N calcular_num ( N1,N2) Variables X, Y, N V N2 < 10 F N
import java.io.*; public class ejem_metodos9 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N1, N2, N; N1= leernum( ); N2= leernum( ); N = calcular_num(N1,N2); reportar(N); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N<1 || N>99); return N; } /*******proceso de calcular*****/ static int calcular_num (int N1, int N2) throws IOException
{ int X,Y, N; if( N2 < 10) { X = N1*10 + N2; } else{ X = N1*100 + N2; } if( N1 < 10) { Y = N2*10 + N1; } else{ Y = N2*100 + N1; } if( X > Y) { N=X; } else{ N =Y; } return N ; }
X = N1 * 10 + N2
X = N1 * 100 + N2
N1 < 10
Y = N2 * 10 + N1
Y = N2 * 100 + N1
V
N=X retornar N
X>Y
N=Y
/******** reporte de resultados******/ static void reportar ( int N) throws IOException {System.out.println("El Nmero es = " +N); return ; } }
10
10.- Leer un nmero entero positivo N, invertirlo y reportar el nmero N y el nmero invertido
Inicio Variables N, Ninv
import java.io.*; public class ejem_metodos10 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N, Ninv; N= leenum( ); Ninv = invertir(N); reportar(N, Ninv); }
/*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N<=0); return N; } /*******proceso de calcular*****/ static int calcular (int N) throws IOException { int Ninv=0, res; while( N>0) {res = N%10; Ninv=Ninv*10 + res N =N/10; } return Ninv ; } /******** reporte de resultados******/ static void reportar ( int N, int Ninv) throws IOException {System.out.println("No. = " +N + "Invertido = " + Ninv) ; return ; } }
calcular ( N) Variables i , Ninv =0 N>0 res = N %10 Ninv = Ninv*10 + res N = N/10 retornar sum reportar ( N, Ninv) Variables Escribir N, Ninv retornar
11
11.- Leer un nmero entero positivo de 4 dgitos N y separarlo en dos nmeros N1 y N2 de dos dgitos cada uno, luego reportar los nmeros N1 y N2. Inicio Variables N, N1 ,N2
import java.io.*; public class ejem_metodos11 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N, N1,N2 ; N= leernum( ); N1 = separa1(N); N2 = separa2(N); reportar(N1, N2); } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero"); N = Integer.parseInt(br.readLine()); }while(N<1000 || N>9999); return N; } /*******proceso de separar1*****/ static int separa1 (int N) throws IOException { int N1; N1 = N/100; return N1 ; } /*******proceso de separar2*****/ static int separa2 (int N) throws IOException { int N2; N2 = N%100; return N2 ; } /******** reporte de resultados******/ static void reportar ( int N, int N2) throws IOException {System.out.println("Num1 = " +N1 + " Num2 = " + N2) ; return ; } }
N = leernum ( ) N1 =separa1 (N) N2=separa2 (N) reportar ( N1, N2 ) Fin leernum( ) Variables Leer N N 0 retornar N separa1 ( N) Variables N1 N
N2= N %100 retornar N2 reportar ( N1, N2) Variables Escribir N1, N2 retornar
12
12.-Leer dos nmeros enteros positivos y si son iguales reportar su producto, en caso que sean diferentes reportarlos en orden de mayor a menor.
Inicio Variables N1 ,N2
N1 = leernum ( ) N2 = leernum ( ) iguales (N1,N2) diferentes ( N1, N2 ) Fin leernum( ) Variables Leer N N 0 retornar N iguales ( N1, N2) Variables V p N1=N2 F N
import java.io.*; public class ejem_metodos12 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N, N1,N2 ; N1= leernum( ); N2= leernum(); iguales(N1,N2); diferentes(N1, N2); } /*********Leer N *****/ static int leernum( ) throws IOException {int N; do{System.out.print("Ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N 0); return N; } /*******proceso de comparar igualdad *****/ static void iguales (int N1, int N2) throws IOException { int p; if (N1==N2) { System.out.println("Son iguales ") ; P= N1*N2; System.out.println("El producto es = "+p) ; } return ; } /*******proceso de comparar diferencia*****/ static void diferentes (int N1, int N2) throws IOException { if (N1>N2) { System.out.println("May: " + N1 + "Men: " +N2) ; } if (N1<N2) { System.out.println("May: " + N2 + "Men: " +N1) ; } return ; } }
13
Esc. son iguales P= N1 * N2 Escribir p retornar diferentes ( N1, N2) Variables V N1>N2 F
13.-Leer dos nmeros enteros positivos y si son iguales reportar su producto, en caso que sean diferentes reportarlos en orden de mayor a menor. (Otra forma de resolver el mismo problema anterior) import java.io.*; public class ejem_metodos13 Inicio { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N1 ,N2 N1 = leernum ( ) N2 = leernum ( ) V N1=N2 F
/********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N, N1,N2 ; N1= leernum( ); N2 = leernum(); if (N1==N2) { prod (N1,N2); } else { ordena(N1, N2); } } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero :"); N = Integer.parseInt(br.readLine()); }while(N 0); return N; } /*******proceso de comparar igualdad *****/ static void prod (int N1, int N2) throws IOException { int p; System.out.println("Son iguales ") ; p= N1*N2; System.out.print("El producto es = " + p) ; } return ; } /*******proceso de comparar y ordenar*****/ static void ordena (int N1, int N2) throws IOException { if (N1>N2) { System.out.println("May: "+ N1 + " Men: " +N2) ; } else { System.out.println("May: " + N2 + " Men: " +N1) ; } return ; } }
retornar
14
14.-Leer un nmero entero positivo N. Si N es par, calcular y reportar su factorial sino calcular e imprimir sus divisores y reportar cuantos son en total.
Inicio Variables N
import java.io.*; public class ejem_metodos14 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); /********mtodo principal *****/ public static void main(String [ ] args) throws IOException { int N ; N= leernum( ); if (N%2==0) { factorial(N); } else { divisores(N); } } /*********Leer N *****/ static int leernum( ) throws IOException { int N; do{System.out.print("Ingrese un numero :"); N = Integer.parseInt(br.readLine()); }while(N 0); return N; } /*******proceso de calcular factorial *****/ static void factorial (int N) throws IOException { int f=1, i; for(i=1; i<=N; i++) { f = f * i; } System.out.print("El factorial de " +N+ " es = " + f) ; return ; } /*******proceso de calcular divisores*****/ static void divisores (int N) throws IOException { int i, cd=0; System.out.println("Los divisores de " +N+ "son : ") ; for(i=1; i<=N; i++) { if (N% i==0) { System.out.print("\t" +i); cd++; } } System.out.println("\nTotal de divisores = " +cd) ; return ; } }
N = leernum ( ) V N%2=0 F
factorial( N)
divisores((N)
i=1 ; i N;
V Escribir: i cd = cd+1 Escribir : cd
i = i+1
F
N % i= 0
retornar
15
15.-Leer un nmero entero positivo N. Si N es par, calcular y reportar su factorial sino calcular e imprimir sus divisores y reportar cuantos son en total.(Otra forma de resolver el problema)
Inicio Variables N, f , cd import java.io.*; public class ejem_metodos15 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N = leernum ( ) V
f=factorial( N)
N%2=0
cd= divisores((N)
f= f*i retornar f
i=1 ; i N;
V Escribir: i cd = cd+1
N% i = 0
retornar cd
16