Cousins, odd and even in Java

2

I want a program that shows ask the user for numbers until you enter a "0" or a negative number, when this happens, the program should show: 1.-How many numbers are entered 2. How many are cousins 3. How many are pairs 4.- How many are odd.

I have made the code for cases 2,3, and 4. Can I join the code that I already have in some way? Honestly I have no idea what I need or where to proceed, since it is my first Java course. What should I learn to be able to solve this?

I have code for prime numbers:

package nprimo;
import java.util.Scanner;

public class Nprimo {

    public static void main(String[] args) {
        int temp;
        boolean isPrime=true;
        Scanner scan= new Scanner(System.in);
        System.out.println("Ingrese un numero");
        int num = scan.nextInt();
        for (int i=2;i<=num/2;i++)
        {
            temp=num%i;
            if (temp==0)
            {
                isPrime=false;
                break;
            }
        }
        if(isPrime)
            System.out.println(num +"Es un numero primo :) ");
        else 
            System.out.println(num +"No es un número primo :( ");
    }
}

and the code to know if the number is even or odd.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
String m="";
System.out.println("*Determinarn numero es par o impar*");
System.out.println("");
System.out.println("ingreseumero");
Scanner teclado=new Scanner(System.in);
n=teclado.nextInt();
if(n%2==0)
m="es par";
else
m="es impar";
System.out.println("elro "+n+" "+m);
}
}

Thank you.

    
asked by P. Garcìa 03.08.2017 в 01:27
source

2 answers

4

Well, you can have 'IsPrimo', 'IsPar', 'IsImpar', as three Boolean functions (that return 'true' or 'false' as a result). So, to count the number of numbers you enter, you can have a while with a counter inside ... While to count if it's even, odd or cousin, you can have different counters that increase if the specific condition is met. If it is a prime, the prime number counter will increase, and so on.

import java.util.Scanner;


public class Main{
    public static void main (String [] args){
        int contador = 0, contadorPrimo =0, contadorPar = 0;
        int contadorImpar = 0, n = 0;

        Scanner teclado=new Scanner(System.in); 
        //Declaro el contador aqui, para no hacerlo cada vez que empiece el                 
         //ciclo

        do{
            System.out.println("Ingrese un numero: "); 
            n=teclado.nextInt();

            if (isPrimo(n)){
                contadorPrimo++;
            } else if (isImpar(n)){ 
                contadorImpar++; 
            }else if (isPar(n)){
                contadorPar++;
            }
            contador ++; 
        }while(n>0); 
        System.out.println("Cantidad de numeros: " +contador); 
        System.out.println("Cantidad de numeros primos: " + contadorPrimo);
        System.out.println("Cantidad de numeros pares: " + contadorPar);
        System.out.println("Cantidad de numeros impares: " + contadorImpar);
    }


    public static boolean isPrimo(int num){
        int temp = 0;
        for (int i=2;i<=num/2;i++)
        {
            temp=num%i;
            if (temp==0)
            {
              return false; 
            }
        }
        return true;
    }

    public static boolean isImpar(int num) { 
        if(num%2==0){
            return false;
        }else 
            return true; 
    }

    public static boolean isPar(int num) { 
        if(num%2==0){
            return true;
        }else 
            return false; 
        }

}

Those if's and else if's call the functions and send them as parameter 'n' (the number you entered) If the result they return is 'true' then the sentence will be fulfilled in the if.

I do not know if you want me to count '0' or the negative number as a number, if so, then you should change these two:

     System.out.println("Cantidad de numeros: " + (contador-1)); 
     System.out.println("Cantidad de numeros primos: " + (contadorPrimo-1));

The '1' that you subtract from the number counter is the '0' that you set to exit the do while. And in the case of'Primer counter 'it is because, in this case, the 0 is being considered a prime number.

    
answered by 03.08.2017 / 03:57
source
0

I believe that the response of @Adalgisa Ferrer is valid, then I present a variation to the answer given by @Adalgisa Ferrer

import java.util.Scanner;

public class ContarNumeros {
public static void main (String [] args){
    int contador = 0, contadorPrimo =0, contadorPar = 0;
    int contadorImpar = 0, n = 0;

    Scanner teclado=new Scanner(System.in); 
    //Declaro el contador aqui, para no hacerlo cada vez que empiece el                 
     //ciclo

    do{
        System.out.println("Ingrese un numero: "); 
        n=teclado.nextInt();

        if (isPar(n)){
            contadorPar++;
            if(n == 2){
                contadorPrimo++;
            }
        } else{ 
            contadorImpar++;
            if(n != 1 && isPrimo(n)){
                contadorPrimo++;
            }
        }
        contador ++; 
    }while(n>0); 
    System.out.println("Cantidad de numeros: " +contador); 
    System.out.println("Cantidad de numeros primos: " + contadorPrimo);
    System.out.println("Cantidad de numeros pares: " + contadorPar);
    System.out.println("Cantidad de numeros impares: " + contadorImpar);
}

public static boolean isPrimo(int num){
    boolean primo = true;
    int maxvalue = (int)(Math.sqrt(num));
    for (int i=3; i <= maxvalue; i = i+2)
    {

        if (num%i == 0)
        {
          primo = false;
          break;
        }
    }
    return primo;
}

public static boolean isPar(int num) { 
    boolean par = true;
    if(num%2 != 0){
        par = false;
    }
    return par;
}
}

The changes are as follows:

  • We only leave the method that validates if it is an even number, if it is not even, it is odd.
  • If it is even, we validate if it is number 2, if so, we increase the prime number counter by 1.
  • The validation of if it is a cousin is only carried out if the number is odd and different from 1.
  • In the isPrimo(n) method, we decrease the validation range to the square root of n starting from the number 3 and only validate with odd numbers.
  • If there is an odd divisor for the number n then n is not prime and we get out of the loop.
  • answered by 04.08.2017 в 19:30