Está en la página 1de 16

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

PROBLEMAS-METODOS 1.-Leer un nmero entero positivo y calcular su factorial


import java.io.*; public class ejem_1 { public static void main(String []args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, fac=1, i ; do{ System.out.print("ingrese un numero : "); N = Integer.parseInt(br.readLine()); } while(N<=0); for ( i =1 ; i<=N ; 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)); /*********mtodo principal*****/ public static void main(String []args) throws IOException { int N, fac; N= leenum(); fac = factorial(N); reportar(N,fac); } /*********ingreso se datos*****/ static int leenum( ) throws IOException { int N; do{System.out.print("ingrese un numero : "); N = Integer.parseInt(br.readLine()); }while(N<=0); return N; } /*******proceso de factorial*****/ static int factorial(int N) throws IOException { int i , fac=1; for( i=1 ; i<=N ; i++) {fac = fac * i; } return fac ; } /******** reporte de resultados******/ static void reportar(int N ,int fac) { System.out.println("El factorial de +N+ es = " +fac); return ; } }

a.-Sin Utilizar mtodos


Inicio Variables N , i, fac = 1

Leer N N 0 i=1; i N fac = fac * i i = i +1

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

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

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

WALTER LAZO AGUIRRE METODOS-III

Inicio Variables X, N , i, pot = 1

Leer N N 0 Leer X X 0 i=1; i N pot = pot * X Escribir : X,N,pot fin i = i +1

for( i=1 ; i<=N ; i++) { pot = pot * X; } System.out.println("La potencia de "+X+" a la "+ N + es = " +pot); } }

b.- Utilizando mtodos


Inicio Variables X,N,pot X = leenum( ) N = leenum( ) pot = potencia(X,N ) reportar (X,N,pot) Fin reportar (X,N,pot) Variables Escribir: X, N, pot retornar leenum ( ) Variables Nu Leer Nu Nu 0 retornar Nu potencia (X,N) Variables i, pot=1 i=1 ; i N; i=i+1 pot = pot * X retornar 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 }

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

Inicio Variables N, i , cdiv =0 Leer N N 0 i = 1; V i N ; i = i +1 F

N%i=0

Escribir : i cdiv = cdiv +1

Escribir : N, cdiv

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

b.Utilizando mtodos Inicio Variables 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

Escribir : i cdiv = cdiv +1 retornar cdiv

reportar( N, cdiv) Variables Escribir ; N, cdiv retornar

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

N = leernum( ) sum= calcular( N ) reportar ( sum ) Fin

leernum( ) Variables Leer N N 0 Retornar N N

calcular ( N) Variables i , sum =0 I=1; i N ; i = i+1 V N%i=0 F

Escribir: i sum = sum + i

retornar sum reportar ( sum) Variables Escribir sum retornar

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

import java.io.*; public class ejem_metodos5 {static BufferedReader br=new BufferedReader(new

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

calcular_pro ( N1,N2) Variables i , pro =0 i=1; i N1 ; i = i+1 pro = pro + N2

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

reportar (N1,N2, pro) Variables Escribir : N1,N2,pro

return ; } }

retornar

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

leernum( ) Variables Leer N N <10 V N >99 retornar N N

calcular_num ( N1,N2) Variables N N = N1 * 100 + N2 retornar N

/*******proceso de calcular*****/ static int calcular_num (int N1, int N2) throws IOException { int N; N = N1*100 + N2; return N ; }

reportar ( N ) Variables Escribir : N retornar

/******** reporte de resultados******/ static void reportar ( int N) throws IOException {System.out.println("El Nmero es = " +N); return ; } }

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

reportar ( N ) Variables Escribir : N retornar

/******** reporte de resultados******/ static void reportar ( int N) throws IOException {System.out.println("El Nmero es = " +N); return ; } }

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

leernum( ) Variables Leer N N <10 V N >99 retornar N N

calcular_num ( N1,N2) Variables N V N1 >N2 F

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

reportar ( N ) Variables Escribir : N retornar

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

reportar ( N ) Variables Escribir : N retornar

/******** reporte de resultados******/ static void reportar ( int N) throws IOException {System.out.println("El Nmero es = " +N); return ; } }
10

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

N = leernum( ) Ninv= invertir( N ) reportar ( N, Ninv ) Fin

leernum( ) Variables Leer N N 0 retornar N 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*****/ 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

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

N1= N /100 retornar N1 separa2 ( N) Variables N2

N2= N %100 retornar N2 reportar ( N1, N2) Variables Escribir N1, N2 retornar

12

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

Escribir: N1,N2 V N1<N2


F

Escribir: N2,N1 retornar

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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

prod( N1, N2 ) ordena(N1,N2)

Fin leernum( ) Variables Leer N N 0 retornar N prod( N1, N2) Variables p N

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

Escribir: son iguales P= N1 * N2 Escribir : p retornar

ordena ( N1, N2) Variables V N1>N2 F

Escribir: N1,N2 Escribir: N2,N1

retornar

14

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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)

Fin leernum( ) Variables Leer N N 0 retornar N factorial( N) Variables f=1, i i = i+1 N

i=1 ; i N; f= f*i Escribir : f retornar divisores( N) Variables i, cd=0

i=1 ; i N;
V Escribir: i cd = cd+1 Escribir : cd

i = i+1
F

N % i= 0

retornar

15

UNIVERSIDAD PRIVADA ANTENOR ORREGO INTRODUCCIN A LA PROGRAMACIN

WALTER LAZO AGUIRRE METODOS-III

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)

/********mtodo principal *****/


F public static void main(String [ ] args) throws IOException { int N , f, cd; N= leernum( ); if (N%2==0) { f= factorial(N); reportar("El factorial es = ", f ); } else { cd= divisores(N); reportar("No. de divisores = ", cd ); } } /*********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 int factorial (int N) throws IOException { int f=1, i; for(i=1; i<=N; i++) { f = f * i; } return f ; } /*******proceso de calcular divisores*****/ static int 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++; } } return cd ; } /*******proceso de reportar mensaje y valor numrico****/ static void reportar (String mensaje , int X) throws IOException { System.out.println(" \t "+ mensaje + " \t "+X) ; } }

N%2=0

cd= divisores((N)

reportar(factorial reportar(No. de es = , f ) divisores=, cd)

Fin leernum( ) Variables Leer N N 0 retornar N N factorial( N)


Variables f=1, i i=1;iN ; i=i+1

f= f*i retornar f

divisores( N) Variables i, cd=0 i = i+1


F

i=1 ; i N;
V Escribir: i cd = cd+1

N% i = 0

retornar cd

reportar (mensaje, X) Variables Escribir: mensaje, X retornar

16

También podría gustarte