Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Square prime numbers up to 50 and then round to nearest integer plz help

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Square prime numbers up to 50 and then round to nearest integer plz help

    What I need to do is square all the prime numbers up to 50 and then round to nearest integer. Here is the code I have so far.

    public class Test33
    {
    public static void main (String[] args)
    {
    int nValues = 50;

    OuterLoop:
    for(double i = 2; i <= nValues; ++i)
    {
    for(double j = 2; j < i; ++j)
    {
    if(i%j == 0)
    {
    continue OuterLoop;
    }
    }
    System.out.println( Math.sqrt( i ) );
    }
    }
    }




    It squares all the primes, but I can't figure out how to round them. I think I need to use Math.round.
    ANY help would be appreciated.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Square prime numbers up to 50 and then round to nearest integer plz help

    To round a floating point number you simply add 0.5 and typecast to int.
    If you want to round the variable "someDouble" you can do:
    int roundedValue = (int) someDouble + 0.5;

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Square prime numbers up to 50 and then round to nearest integer plz help

    Quote Originally Posted by JKAA View Post
    I think I need to use Math.round.
    What happened when you tried?

    Welcome to the forum
    See the Announcements page for the use of code tags.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Square prime numbers up to 50 and then round to nearest integer plz help

    All prime numbers are positive integers. Squaring an integer always results in another integer. Addition of integers always results in integers. I don't see why rounding is required at all.

    Your code is using the sqrt() function, which is the inverse of the square operation, which can have non-integer return values for integer input.

    Take out a piece of paper and write down what steps you need to take (in plain english, or whatever natural language you are most comfortable using) to accomplish this task.

Similar Threads

  1. [SOLVED] Confused on Prime numbers
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2013, 03:26 AM
  2. [SOLVED] How to make Math.round() round to the nearest .1?
    By bdennin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 2nd, 2013, 07:23 PM
  3. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  4. trying to generate prime numbers
    By yingyang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2011, 12:21 PM
  5. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM

Tags for this Thread