0
Sum of prime numbers upto 10
please explain step by step
3 Respuestas
+ 2
A number is a prime, if its not divisible by any number. Hence in this foe loop, you have your number divided by every numbee less than the number itself. If it is divisible, Return true or false.
+ 1
public class Main {
public static void main(String args[]){
int number = 2;
int count = 0;
long sum = 0;
while(count < 10){
if(isPrimeNumber(number)){
sum += number;
count++;
}
number++;
}
System.out.println(sum);
}
private static boolean isPrimeNumber(int number){
for(int i=2; i<=number/2; i++){
if(number % i == 0){
return false;
}
}
return true;
}
0
provide code