Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Icsi Algoyprog Clase Metodos 3 Rev
Icsi Algoyprog Clase Metodos 3 Rev
1
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
N
2.-Leer dos números enteros positivos X, N y calcular el valor de X por multiplicaciones
sucesivas.
a.- Sin utilizar métodos
import java.io.*;
public class ejem_2
Inicio {
public static void main(String []args) throws IOException
Variables X, N , i, pot = 1 {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, X, pot=1, i;
Leer N do{System.out.print("ingrese un numero :");
N = Integer.parseInt(br.readLine());
N ≤ 0 }while(N<=0);
Escribir : X,N,pot }
}
fin
import java.io.*;
public class ejem_metodos2
b.- Utilizando métodos
{ static BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
/*********método principal*****/
leenum ( ) public static void main(String []args) throws IOException
Inicio
{ int X ,N, pot;
Variables Nu X= leenum( );
Variables X,N,pot N= leenum( );
pot = potencia(X , N);
X = leenum( ) Leer Nu reportar(X, N, pot);
}
N = leenum( ) Nu ≤ 0 /*********ingreso se datos*****/
static int leenum( ) throws IOException
pot = potencia(X,N ) retornar Nu { int Nu;
do{System.out.print("ingrese un numero : ");
Nu = Integer.parseInt(br.readLine());
reportar (X,N,pot)
potencia (X,N) }while(Nu<=0);
return Nu;
Fin }
Variables i, pot=1
/*******proceso de potencia*****/
static int potencia (int X, int N) throws IOException
reportar (X,N,pot) i=1 ; i ≤ N; i=i+1 { int i , pot=1;
for( i=1 ; i<=N ; i++)
Variables pot = pot * X {pot = pot *X;
}
Escribir: X, N, pot retornar pot return pot ;
}
retornar /******** 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
}
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
3.-Leer un número entero positivo N , calcular e imprimir sus divisores y reportar cuanto son en total.
3
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
b.-Utilizando métodos import java.io.*;
public class ejem_metodos3
Inicio {static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Fin
/*******Leer número***********/
leernum( ) static int leernum( ) throws IOException
{ int N;
Variables N do { System.out.print("ingrese un numero :");
N = Integer.parseInt(br.readLine());
Leer N }
while (n<=0);
N ≤ 0 return N;
}
retornar N /*******************/
static int calcula_div ( int N ) throws IOException
{
calcula_div ( N )
int i, cdiv =0;
System.out.println("Los divisores son ");
Variables i , cdiv =0
for (i=0 ; i<= N ; i++)
i = 1; i ≤ N ; i = i +1 {
if (N%i==0)
V N%i=0 F { System.out.print("\t" + i);
cdiv ++;
Escribir : i }
cdiv = cdiv +1 }
return cdiv;
retornar cdiv }
/*******************/
static void reportar ( int N, int cdiv ) throws IOException
reportar( N, cdiv) {
System.out.println("El Total de divisores de "+ N + " es = " + cdiv);
Variables return ;
}
Escribir ; N, cdiv
} // llave de la clase
retornar
4
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
4.- Leer un número entero positivo N, calcular e imprimir sus divisores y reportar la suma de
los divisores.
/*******proceso de calcular*****/
calcular ( N) static int calcular (int N) throws IOException
{ int i , sum=0;
Variables i , sum =0
System.out.println("Los divisores de " +N+ "son : ");
I=1; i ≤ N ; i = i+1 for( i=1 ; i<=N ; i++)
{if ( N% i == 0)
V N%i=0 F { System.out.print ("\t" + i );
sum = sum + i;
Escribir: i }
}
sum = sum + i
return sum ;
}
retornar sum
/******** reporte de resultados******/
static void reportar ( int sum) throws IOException
reportar ( sum) {System.out.println("Suma de los divisores es = " +sum);
Variables
return ;
}
Escribir sum }
retornar
5
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
5.- Leer dos números enteros positivos N1 y N2, calcular el producto por sumas sucesivas y
reportar el producto calculado.
Inicio import java.io.*;
public class ejem_metodos5
Variables N1, N2, pro
{static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N1 = leernum( )
/********método principal *****/
N2 = leernum( ) public static void main(String [ ] args) throws IOException
{ int N1, N2, pro;
pro= calcular_pro( N1,N2 ) N1= leernum( );
N2= leernum( );
reportar ( N1,N2,pro ) pro = calcular_pro(N1,N2);
reportar(pro);
Fin }
calcular_pro ( N1,N2)
/*******proceso de calcular*****/
Variables i , pro =0 static int calcular_pro (int N1, int N2) throws IOException
{ int i , pro=0;
i=1; i ≤ N1 ; i = i+1
6
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
6.- Leer dos números enteros positivos N1, N2, de dos dígitos cada uno, unirlos en un solo número
N =N1N2 y reportar el número obtenido.
Inicio import java.io.*;
public class ejem_metodos6
Variables N1, N2, N
{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N1 = leernum( )
/********método principal *****/
N2 = leernum( ) public static void main(String [ ] args) throws IOException
{ int N1, N2, N;
N= calcular_num( N1,N2 ) N1= leernum( );
N2= leernum( );
reportar ( N) N = calcular_num(N1,N2);
reportar(N);
Fin }
/*********Leer N *****/
leernum( ) static int leernum( ) throws IOException
{ int N;
Variables N do{System.out.print("Ingrese un numero : ");
N = Integer.parseInt(br.readLine());
Leer N }while(N<10 || N>99);
return N;
N <10 V N >99
}
retornar N
/*******proceso de calcular*****/
calcular_num ( N1,N2)
static int calcular_num (int N1, int N2) throws IOException
Variables N
{ int N;
7
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
7.- Leer dos números enteros positivos N1, N2, de uno o dos dígitos cada uno, unirlos en un solo
número N =N1N2 y reportar el número obtenido.
Inicio import java.io.*;
public class ejem_metodos7
Variables N1, N2, N
{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N1 = leernum( )
/********método principal *****/
N2 = leernum( ) public static void main(String [ ] args) throws IOException
{ int N1, N2, N;
N= calcular_num( N1,N2 ) N1= leernum( );
N2= leernum( );
reportar ( N) N = calcular_num(N1,N2);
reportar(N);
Fin }
/*********Leer N *****/
leernum( ) static int leernum( ) throws IOException
{ int N;
Variables N do{System.out.print("Ingrese un numero : ");
N = Integer.parseInt(br.readLine());
Leer N }while(N<1 || N>99);
return N;
N <1 V N >99
}
retornar N
Variables
/******** reporte de resultados******/
Escribir : N static void reportar ( int N) throws IOException
{System.out.println("El Número es = " +N);
retornar
return ;
}
}
8
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
8.- Leer dos números enteros positivos N1, N2, de dos dígitos cada uno, unirlos en un solo número
N =N1N2 o N= N2N1, de tal modo que N tenga el mayor valor posible y reportar el número
obtenido.
Inicio import java.io.*;
public class ejem_metodos8
Variables N1, N2, N
{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N1 = leernum( )
/********método principal *****/
N2 = leernum( ) public static void main(String [ ] args) throws IOException
{ int N1, N2, N;
N= calcular_num( N1,N2 ) N1= leernum( );
N2= leernum( );
reportar ( N) N = calcular_num(N1,N2);
reportar(N);
Fin }
/*********Leer N *****/
leernum( ) static int leernum( ) throws IOException
{
Variables N int N;
do{System.out.print("Ingrese un numero : ");
Leer N N = Integer.parseInt(br.readLine());
}while(N<10 || N>99);
N <10 V N >99
return N;
retornar N }
9
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
9.- Leer dos números enteros positivos N1, N2, de uno o dos dígitos cada uno, unirlos en un solo
número N =N1N2 o N= N2N1, de tal modo que N tenga el mayor valor posible y reportar el
número obtenido.
Inicio import java.io.*;
public class ejem_metodos9
Variables N1, N2, N
{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N1 = leernum( )
/********método principal *****/
N2 = leernum( ) public static void main(String [ ] args) throws IOException
{ int N1, N2, N;
N= calcular_num( N1,N2 ) N1= leernum( );
N2= leernum( );
reportar ( N) N = calcular_num(N1,N2);
reportar(N);
Fin }
calcular ( N)
/*******proceso de calcular*****/
Variables i , Ninv =0 static int calcular (int N) throws IOException
{ int Ninv=0, res;
N>0
while( N>0)
res = N %10 {res = N%10;
Ninv=Ninv*10 + res
Ninv = Ninv*10 + res N =N/10;
N = N/10
}
return Ninv ;
retornar sum }
11
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
11.- Leer un número entero positivo de 4 dígitos N y separarlo en dos números N1 y N2 de dos dígitos cada
uno, luego reportar los números N1 y N2.
leernum( )
/*********Leer N *****/
Variables N static int leernum( ) throws IOException
{
Leer N int N;
do{System.out.print("Ingrese un numero");
N ≤ 0 N = Integer.parseInt(br.readLine());
}while(N<1000 || N>9999);
retornar N
return N;
}
separa1 ( N)
/*******proceso de separar1*****/
Variables N1 static int separa1 (int N) throws IOException
{ int N1;
N1= N /100 N1 = N/100;
return N1 ;
retornar N1
}
12
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
12.-Leer dos números enteros positivos y si son iguales reportar su producto, en caso que sean
diferentes reportarlos en orden de mayor a menor.
Inicio
import java.io.*;
Variables N1 ,N2 public class ejem_metodos12
{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N1 = leernum ( )
retornar
/*******proceso de comparar diferencia*****/
static void diferentes (int N1, int N2) throws IOException
diferentes ( N1, N2)
{
Variables if (N1>N2)
{ System.out.println("May: " + N1 + "Men: " +N2) ;
V N1>N2 F }
if (N1<N2)
Escribir: N1,N2 { System.out.println("May: " + N2 + "Men: " +N1) ;
}
V N1<N2 F
return ;
Escribir: N2,N1
}
}
retornar
13
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
13.-Leer dos números 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.*;
Inicio public class ejem_metodos13
{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N1 ,N2
/********método principal *****/
N1 = leernum ( ) public static void main(String [ ] args) throws IOException
{ int N, N1,N2 ;
N2 = leernum ( ) N1= leernum( );
V N1=N2 F N2 = leernum();
if (N1==N2)
prod( N1, N2 ) ordena(N1,N2) { prod (N1,N2);
}
Fin else { ordena(N1, N2);
}
}
leernum( )
/*********Leer N *****/
Variables N
static int leernum( ) throws IOException
Leer N { int N;
do{System.out.print("Ingrese un numero :");
N ≤ 0 N = Integer.parseInt(br.readLine());
}while(N≤ 0);
retornar N return N;
}
/*******proceso de comparar igualdad *****/
prod( N1, N2)
static void prod (int N1, int N2) throws IOException
Variables p { int p;
System.out.println("Son iguales ") ;
Escribir: “son iguales” p= N1*N2;
System.out.print("El producto es = " + p) ;
P= N1 * N2 }
return ;
Escribir : p
}
retornar /*******proceso de comparar y ordenar*****/
static void ordena (int N1, int N2) throws IOException
{
if (N1>N2)
{ System.out.println("May: "+ N1 + " Men: " +N2) ;
ordena ( N1, N2)
}
Variables else
{ System.out.println("May: " + N2 + " Men: " +N1) ;
V N1>N2 F }
return ;
Escribir: N1,N2 Escribir: N2,N1 }
}
retornar
14
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
14.-Leer un número 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 import java.io.*;
public class ejem_metodos14
Variables N { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
15
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
15.-Leer un número 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 import java.io.*;
public class ejem_metodos15
Variables N, f , cd { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
16