# Finding the sum of prime numbers below the inputted number

Page 1 of 1

## 5 Replies - 2602 Views - Last Post: 09 July 2013 - 06:37 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324362&amp;s=a40daca055aa17976046d953df030c1c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ramyre

Reputation: 0
• Posts: 4
• Joined: 06-July 13

# Finding the sum of prime numbers below the inputted number

Posted 06 July 2013 - 06:51 PM

```import java.util.*;
import java.io.*;
{
public static void main(String[] args)throws IOException{
Scanner input = new Scanner(System.in);
long n = 2;
long sum = 0;
long limit = 0;
while (true)
{
System.out.println("Enter the limit: ");
limit = input.nextLong();
if (limit <= 0){
System.out.println("The number must be positive: ");
limit = input.nextInt();
}
while (n < limit){
if (Prime(n)==true)
{
sum = sum + n;
}
n++;
}
System.out.println("The sum of the primes below " +limit+ " is: " +sum);
}
}
public static boolean Prime(long number)// Checks to see if a number is prime
{
for (int i = 2; i <= number/2; i++) {
if (number % i == 0) // if number is divisible by any number that is <= half of it, then number is NOT prime
{
return false;
}
}
return true; // If number isn't divisible by any number other than itself and one, then number IS prime
}

}
```

This post has been edited by Flukeshot: 06 July 2013 - 07:11 PM
Reason for edit:: code tags

Is This A Good Question/Topic? 0

## Replies To: Finding the sum of prime numbers below the inputted number

### #2 ConciselyVerbose

• D.I.C Regular

Reputation: 91
• Posts: 315
• Joined: 05-July 13

## Re: Finding the sum of prime numbers below the inputted number

Posted 06 July 2013 - 06:53 PM

### #3 ramyre

Reputation: 0
• Posts: 4
• Joined: 06-July 13

## Re: Finding the sum of prime numbers below the inputted number

Posted 06 July 2013 - 06:58 PM

im just a beginner in programming...in this program if your first input is 10 it will display 17 which is correct and if you input again let's say 100 it displays 1060 which is also correct..but if you again input 10 it will display again 1060.

i just want what might be the problem that if i again inputted 10 it displays 1060

i just want to know what might be the problem

### #4 ConciselyVerbose

• D.I.C Regular

Reputation: 91
• Posts: 315
• Joined: 05-July 13

## Re: Finding the sum of prime numbers below the inputted number

Posted 06 July 2013 - 06:59 PM

Did you write this?

### #5 ConciselyVerbose

• D.I.C Regular

Reputation: 91
• Posts: 315
• Joined: 05-July 13

## Re: Finding the sum of prime numbers below the inputted number

Posted 06 July 2013 - 07:07 PM

Where you read in the limit value from the user, you need to reset the n and sum values. What is happening is that when you go to the 100 you use, the sum is set to 1060, and the n is set to the last prime.

```n = 2;
sum = 0;

```

When you go from a lower number to a higher number, it works because it is picking up where the last count left off, but you want to start from scratch every time, or going back down to a lower number will break it.

This post has been edited by ConciselyVerbose: 06 July 2013 - 07:07 PM

### #6 ramyre

Reputation: 0
• Posts: 4
• Joined: 06-July 13

## Re: Finding the sum of prime numbers below the inputted number

Posted 09 July 2013 - 06:37 AM

Sir/Ma'am sorry for my late reply..Yes I made this codes, I followed your directions and it actually works..thank you so much