Programação Progressiva
Gostaria de reagir a esta mensagem? Crie uma conta em poucos cliques ou inicie sessão para continuar.
Programação Progressiva

Fórum para dúvidas dos sites: Programação Progressiva, Java Progressivo, C Progressivo


Você não está conectado. Conecte-se ou registre-se

Desafio: Ache todos os números primos até 1000.

Ir para baixo  Mensagem [Página 1 de 1]

ellalves



Código:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package numerosprimos;

/**
 *
 * @author ellalves
 */

public class NumerosPrimos {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {  
       for(int i = 0; i < 999; i++){
          
           if(isPrimo(i) == true)
           {
               System.out.printf("O número %d é primo \n",i);
           }
       }
    }
  
    /*
    * @param int
    * return boolean
    */
    public static boolean isPrimo(int N){
        boolean sair = false;
        int P = 0; //Inicializa a variavel P
        
        //Se sair = false começa o looping
        if(sair == false) {
            
            //Fa o looping dividindo o numero N por i = 2,3,4,5, ...
            for(int i = 2; i < N; i++) {
                
                //Se N é divisivel por i entao não é primo
                if(N % i == 0) {    
                    P = 1;
                    sair = false;  
                }  
            }
            
            //Se o numero for zero ou um sair = false
            if(N == 0 || N==1) {
                P = 1;
                sair = false;    
            }
            
            //Se P = 0 Não ocorreu nenhum erro e o numero é primo
            if(P == 0)
                sair = true;    
        }
    
        //Retorna true ou false
        return sair;
}

Ir para o topo  Mensagem [Página 1 de 1]

Permissões neste sub-fórum
Não podes responder a tópicos