write a program to calculate the sum of all the prime numbers between the range of 1 and 100
Anonymous:
In which language?
Answers
Answered by
36
✪Hey There✪
Here is Your Program
public class PrimeNumber {
public class PrimeNumber { public static void main(String args[]) {
long sum = 0;
for (int number = 2; number <= 100; number++) {
if (isPrimeNumber(number)) { sum += 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;
}
}
Hope It Helps.
Here is Your Program
public class PrimeNumber {
public class PrimeNumber { public static void main(String args[]) {
long sum = 0;
for (int number = 2; number <= 100; number++) {
if (isPrimeNumber(number)) { sum += 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;
}
}
Hope It Helps.
Answered by
35
Here's a snippet of the logical expressions in Java
int a , c,sum=0, d;
for (a=1; a<=100; a++){
d=0;
for(c=1;c<=a;c++){
if(a%c==0){
d++;
}
if (d==2)
sum=sum+a;
}
}
Haven't run a check, hope it works!
int a , c,sum=0, d;
for (a=1; a<=100; a++){
d=0;
for(c=1;c<=a;c++){
if(a%c==0){
d++;
}
if (d==2)
sum=sum+a;
}
}
Haven't run a check, hope it works!
Similar questions