- 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;
}
Programação Progressiva