5 Replies - 1347 Views - Last Post: 09 July 2013 - 06:37 AM Rate Topic: -----

#1 ramyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 class PrimeNumber 
{
    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  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

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

Posted 06 July 2013 - 06:53 PM

What are you asking?
Was This Post Helpful? 0
  • +
  • -

#3 ramyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • 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.

Add this where you read the limit from the user.

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

Was This Post Helpful? 2
  • +
  • -

#6 ramyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1