You are on page 1of 2

/*1.

-Escribir un programa utilizando m�todos que presente un men� de opciones que


permita elegir y realizar cada una de las siguientes tareas:
a.-Leer un N� N entero positivo
b.-Calcular el factorial de N. Reportar el factorial
c.-Calcular N invertido. Reportar N invertido.
d.-Calcular la suma de los digitos de N. Reportar
e.-Terminar.
*/
package opc;
import java.io.*;
public class OPC {static BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
public static void main(String[] args) throws IOException{
int N=0, fac, Ninv, sum;
char opc;
do{opc=menu();
switch(opc){case 'a': N=LeerN("Ingrese N: ");
break;
case 'b': if(N!=0)
{fac=Factorial(N);
Reportar("Factorial",fac);
}
else{Mensaje();
}
break;
case 'c': if(N!=0)
{Ninv=Invertido(N);
Reportar("N� invertido", Ninv);
}
else{Mensaje();
}
break;
case 'd': if(N!=0)
{sum=suma(N);
Reportar("Suma de digitos :", sum);
}
else{Mensaje();
}
break;
case 'e':Terminar();
break;

}
while(opc!='e');
}
/////////////////////////////////MENU///////////////////////////////////////////
static char menu()throws IOException
{char opc;
do{System.out.println("Menu");
System.out.println("a.-Leer N:");
System.out.println("b.-Calcular el factorial :");
System.out.println("c.-Invertir el n�mero : ");
System.out.println("d.-Suma de sus d�gitos");
System.out.println("e.-Terminar ");
System.out.println("Seleccione una opci�n del [a-e] : ");
opc=br.readLine().toLowerCase().charAt(0);
}
while (opc<'a'||opc>'e');
return opc;
}
///////////////////////LEER N//////////////////////////////////////////////////
static int LeerN(String Mensaje)throws IOException
{int N;
do{System.out.print(Mensaje);
N=Integer.parseInt(br.readLine());
}
while(N<=0);
return N;
}
//////////////////////Factorial////////////////////////////////////////////////
static int Factorial(int N)throws IOException
{int i, fac=1;
for(i=1;i<=N;i++)
{fac=fac*i;
}
return fac;
}
///////////////////////Invertir el n�mero//////////////////////////////////////
static int Invertido(int N)throws IOException
{int Ninv=0, res;
while(N>0)
{res=N%10;
Ninv=Ninv*10+res;
N=N/10;
}
return Ninv;
}
/////////////////////Suma de digitos//////////////////////////////////////////
static int suma(int N)throws IOException
{int res, sd=0;
while(N>0)
{res=N%10;
sd=sd+res;
N=N/10;
}
return sd;
}
//////////////////////Reportar/////////////////////////////////////////////////
static void Reportar (String Mensaje, int X)throws IOException
{System.out.println("\n"+Mensaje+" = "+X);
return;
}
/////////////////////Mensaje//////////////////////////////////////////////////
static void Mensaje()throws IOException
{System.out.println("\nNo ha ingresado datos....");
return;
}
////////////////Terminar/////////////////////////////////////////////////////
static void Terminar()throws IOException
{System.out.println("\nGracias por usar el programa!");
return;
}

You might also like