Está en la página 1de 16

UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE

INGENIERIA DE COMPUTACIÓN Y SISTEMAS


ALGORITMIA Y PROGRAMACIÓN METODOS III
PROBLEMAS-METODOS

1.-Leer un número entero positivo y calcular su factorial

a.-Sin Utilizar métodos import java.io.*;


public class ejem_1
Inicio { public static void main(String []args) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N , i, fac = 1 int N, fac=1, i ;
do{ System.out.print("ingrese un numero : ");
Leer N N = Integer.parseInt(br.readLine());
}
N ≤ 0 while(N<=0);

i=1; i ≤ N i = i +1 for ( i =1 ; i<=N ; i++)


{ fac = fac * i;
fac = fac * i }

System.out.println("El factorial de “+N+” es = " +fac);


}
Escribir : N,fac }

fin import java.io.*;


public class ejem_metodos1
{ static BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
b.-Utilizando métodos /*********método principal*****/
public static void main(String []args) throws IOException
leenum ( ) { int N, fac;
Inicio N= leenum();
Variables N fac = factorial(N);
Variables N, fac reportar(N,fac);
}
N = leenum( ) Leer N /*********ingreso se datos*****/
static int leenum( ) throws IOException
fac = factorial(N ) N ≤ 0 { int N;
do{System.out.print("ingrese un numero : ");
reportar (N,fac) retornar N N = Integer.parseInt(br.readLine());
}while(N<=0);
return N;
Fin }
factorial (N)
/*******proceso de factorial*****/
static int factorial(int N) throws IOException
Reportar ( N, fac) Variables i, fac=1
{ int i , fac=1;
for( i=1 ; i<=N ; i++)
Variables i=1 ; i ≤ N; i=i+1 {fac = fac * i;
}
Escribir N, fac fac = fac * i return fac ;
}
retornar retornar fac /******** reporte de resultados******/
static void reportar(int N ,int fac)
{ System.out.println("El factorial de “+N+” es = " +fac);
return ;
}
}

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);

do{System.out.print("ingrese un numero : ");


Leer X X = Integer.parseInt(br.readLine());
}while(X<=0);
X ≤ 0
for( i=1 ; i<=N ; i++)
i=1; i ≤ N i = i +1 { pot = pot * X;
}
pot = pot * X System.out.println("La potencia de "+X+" a la "+ N + “ es = " +pot);

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.

a.-Primero sin usar métodos


import java.io.*;
public class ejem_3
Inicio { public static void main(String []args) throws IOException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N, i , cdiv =0 int N, i, cdiv=0;

do { System.out.print("ingrese un numero :");


Leer N
N = Integer.parseInt(br.readLine());
}
N ≤ 0 while(N<=0);
System.out.println("Los divisores son :");
i = 1; i ≤ N ; i = i +1 for ( i =1 ; i<=N ; i++)
{
V N%i=0 F if (N%i == 0)
{ System.out.print("\t" + i );
Escribir : i cdiv ++;
}
cdiv = cdiv +1 }

System.out.println("El Total de divisores de "+ N + " es = " + cdiv);


Escribir : N, cdiv }
}
Fin

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));

Variables N, cdiv /******* método principal ***********/


public static void main(String []args) throws IOException
N = leernum( ) { int N, cdiv;
N = leernum( );
cdiv = calcula_div( N) cdiv = calcula_div( N);
reportar( N, cdiv)
reportar( N, cdiv) }

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.

Inicio import java.io.*;


public class ejem_metodos4
Variables N, sum { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N = leernum( ) /********método principal *****/
public static void main(String [ ] args) throws IOException
sum= calcular( N )
{ int N, sum;
reportar ( sum ) N= leenum();
sum = calcular(N);
Fin reportar(sum);
}

leernum( ) /*********Leer N *****/


static int leernum( ) throws IOException
Variables N {
int N;
Leer N do{System.out.print("Ingrese un numero :");
N = Integer.parseInt(br.readLine());
N ≤ 0
}while(N<=0);
Retornar N return N;
}

/*******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 }

leernum( ) /*********Leer N *****/


static int leernum( ) throws IOException
Variables N {
int N;
Leer N do{System.out.print("Ingrese un numero :");
N = Integer.parseInt(br.readLine());
N ≤ 0 }while(N<=0);
return N;
retornar N
}

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

pro = pro + N2 for( i=1 ; i<=N1 ; i++)


{
pro = pro + N2;
}
retornar pro return pro ;
}

/******** reporte de resultados******/


reportar (N1,N2, pro)
static void reportar ( int N1,int N2, int pro) throws IOException
Variables
{System.out.println("El producto de " +N1+"*"+N2+"es="+pro);
return ;
Escribir : N1,N2,pro }
}
retornar

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;

N = N1 * 100 + N2 N = N1*100 + N2;


return N ;
retornar N }

reportar ( N ) /******** reporte de resultados******/


static void reportar ( int N) throws IOException
Variables {System.out.println("El Número es = " +N);
return ;
Escribir : N }
}
retornar

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

calcular_num ( N1,N2) /*******proceso de calcular*****/


static int calcular_num (int N1, int N2) throws IOException
Variables N
{ int N;
V N2 < 10 F if( N2 < 10)
{N = N1*10 + N2;
N = N1 * 10 + N2 N = N1 * 100 + N2 }
else{ N = N1*100 + N2;
retornar N
}
return N ;
}
reportar ( 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 }

calcular_num ( N1,N2) /*******proceso de calcular*****/


static int calcular_num (int N1, int N2) throws IOException
Variables N { int N;
if( N1 > N2)
V N1 >N2 F {N = N1*100 + N2;
N = N1*100 + N2 N = N2 * 100 + N1 }
else{ N = N2*100 + N1;
retornar N }
return N ;
}

reportar ( N ) /******** reporte de resultados******/


static void reportar ( int N) throws IOException
Variables { System.out.print("El Número es = " +N);
Escribir : N return ;
}
retornar }

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 }

leernum( ) /*********Leer N *****/


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<1 || N>99);
N <1 V N >99 return N;
}
retornar N
/*******proceso de calcular*****/
static int calcular_num (int N1, int N2) throws IOException
calcular_num ( N1,N2) { int X,Y, N;
if( N2 < 10)
Variables X, Y, N { X = N1*10 + N2;
}
V N2 < 10 F else{ X = N1*100 + N2;
}
X = N1 * 10 + N2 X = N1 * 100 + N2 if( N1 < 10)
{ Y = N2*10 + N1;
V N1 < 10 F }
else{ Y = N2*100 + N1;
Y = N2 * 10 + N1 Y = N2 * 100 + N1
}
if( X > Y)
V X>Y F { N=X;
}
N=X N=Y
else{ N =Y;
retornar N }
return N ;
}
reportar ( N ) /******** reporte de resultados******/
static void reportar ( int N) throws IOException
Variables
{System.out.println("El Número es = " +N);
Escribir : N return ;
}
retornar
retornar N }
10
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERIA DE COMPUTACIÓN Y SISTEMAS
ALGORITMIA Y PROGRAMACIÓN METODOS III
10.- Leer un número entero positivo N, invertirlo y reportar el número N y el número invertido

Inicio import java.io.*;


public class ejem_metodos10
Variables N, Ninv { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N = leernum( )
/********método principal *****/
Ninv= invertir( N ) public static void main(String [ ] args) throws IOException
{ int N, Ninv;
reportar ( N, Ninv ) N= leenum( );
Ninv = invertir(N);
Fin reportar(N, Ninv);
}

leernum( ) /*********Leer N *****/


static int leernum( ) throws IOException
Variables N {
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;
}

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 }

reportar ( N, Ninv) /******** reporte de resultados******/


static void reportar ( int N, int Ninv) throws IOException
Variables {System.out.println("No. = " +N + "Invertido = " + Ninv) ;
return ;
Escribir N, Ninv
}
retornar }

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.

Inicio import java.io.*;


public class ejem_metodos11
Variables N, N1 ,N2 { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
N = leernum ( )
/********método principal *****/
N1 =separa1 (N) public static void main(String [ ] args) throws IOException
{ int N, N1,N2 ;
N2=separa2 (N) N= leernum( );
N1 = separa1(N);
reportar ( N1, N2 ) N2 = separa2(N);
reportar(N1, N2);
Fin }

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
}

separa2 ( N) /*******proceso de separar2*****/


static int separa2 (int N) throws IOException
Variables N2 { int N2;
N2 = N%100;
N2= N %100
return N2 ;
retornar N2 }

/******** reporte de resultados******/


reportar ( N1, N2) static void reportar ( int N, int N2) throws IOException
{System.out.println("Num1 = " +N1 + " Num2 = " + N2) ;
Variables return ;
Escribir N1, N2
}
}
retornar

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 ( )

N2 = leernum ( ) /********método principal *****/


public static void main(String [ ] args) throws IOException
iguales (N1,N2) { int N, N1,N2 ;
N1= leernum( );
diferentes ( N1, N2 ) N2= leernum();
iguales(N1,N2);
Fin diferentes(N1, N2);
}
leernum( )
/*********Leer N *****/
Variables N static int leernum( ) throws IOException
{int N;
Leer 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 *****/
iguales ( N1, N2)
static void iguales (int N1, int N2) throws IOException
Variables p { int p;
if (N1==N2)
V N1=N2 F { System.out.println("Son iguales ") ;
P= N1*N2;
Esc. “son iguales” System.out.println("El producto es = "+p) ;
}
P= N1 * N2
return ;
Escribir p }

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));

N = leernum ( ) /********método principal *****/


public static void main(String [ ] args) throws IOException
V N%2=0 F { int N ;
factorial( N) divisores((N) N= leernum( );
if (N%2==0)
Fin { factorial(N);
}
leernum( ) else { divisores(N);
}
Variables N }
/*********Leer N *****/
Leer N static int leernum( ) throws IOException
N ≤ 0 { int N;
do{System.out.print("Ingrese un numero :");
retornar N N = Integer.parseInt(br.readLine());
}while(N≤ 0);
return N;
factorial( N) }
/*******proceso de calcular factorial *****/
Variables f=1, i
static void factorial (int N) throws IOException
i=1 ; i ≤ N; i = i+1 { int f=1, i;
for(i=1; i<=N; i++)
f= f*i { f = f * i;
}
Escribir : f System.out.print("El factorial de " +N+ " es = " + f) ;
return ;
retornar }
/*******proceso de calcular divisores*****/
divisores( N) static void divisores (int N) throws IOException
{ int i, cd=0;
Variables i, cd=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);
cd++;
Escribir: i }
cd = cd+1 }
System.out.println("\nTotal de divisores = " +cd) ;
return ;
Escribir : cd
}
retornar }

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));

N = leernum ( ) /********método principal *****/


public static void main(String [ ] args) throws IOException
V N%2=0 F { int N , f, cd;
N= leernum( );
f=factorial( N) cd= divisores((N) if (N%2==0)
{ f= factorial(N);
reportar(“factorial reportar(“No. de reportar("El factorial es = ", f );
es = “ , f ) divisores=”, cd) }
else { cd= divisores(N);
Fin reportar("No. de divisores = ", cd );
}
}
leernum( ) factorial( N) /*********Leer N *****/
static int leernum( ) throws IOException
Variables N Variables f=1, i { int N;
do{System.out.print("Ingrese un numero :");
Leer N i=1;i≤N ; i=i+1
N = Integer.parseInt(br.readLine());
f= f*i }while(N<= 0);
N ≤ 0 return N;
retornar f }
retornar N
/*******proceso de calcular factorial *****/
static int factorial (int N) throws IOException
{ int f=1, i;
for(i=1; i<=N; i++)
divisores( N) { f = f * i;
}
Variables i, cd=0
return f ;
}
i=1 ; i ≤ N; i = i+1
/*******proceso de calcular divisores*****/
V N% i = 0 F static int divisores (int N) throws IOException
{ int i, cd=0;
Escribir: i System.out.println("Los divisores de " +N+ " son : ") ;
for(i=1; i<=N; i++)
cd = cd+1 { if (N% i==0)
{ System.out.print("\t" + i);
cd++;
retornar cd }
}
return cd ;
reportar (mensaje, X) }
Variables /*******proceso de reportar mensaje y valor numérico****/
Escribir: mensaje, X static void reportar (String mensaje , int X) throws IOException
retornar {
System.out.println(" \t "+ mensaje + " \t "+X) ;
}
}

16

También podría gustarte