Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tp0Modulo;
- import java.util.Scanner;
- public class Punto4 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Punto4Primos obj = new Punto4Primos();
- int numero = validarNumero(sc, "ingrese un numero positivo");
- System.out.println("el numero " + numero + " es primo? " + obj.esPrimo(numero));
- }
- public static int validarNumero(Scanner valorIngresado, String mensaje) {
- int numero;
- String linea;
- while (true) {
- try {
- System.out.println(mensaje);
- linea = valorIngresado.nextLine();
- numero = Integer.parseInt(linea);
- break;
- } catch (Exception e) {
- System.out.println("ERROR!!! INGRESE UN NUMERO POSITIVO");
- }
- }
- return numero;
- }
- }
- ########esta parte copiar en una nueva clase ########
- package tp0Modulo;
- public class Punto4Primos {
- public boolean esPrimo(int x) {
- boolean primo = true;
- if (x == 1 || ((x % 2) == 0 && x > 2)) {
- primo = false;
- } else {
- for (int i = 2; i < (x / 2); i++) {
- if (x % i == 0) {
- primo = false;
- break;
- }
- }
- }
- return primo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement