Results 1 to 6 of 6
  1. #1
    kurap3ka is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Adding squared numbers in a loop

    ok ill try to explain my problem as best as i can.

    Im required to create a program that sums up the squares beginning from 1 to the number the user entered, for example the number is 3 so the output will be 14 (because 1squared +2squared+ 3squared or 1+4+9=14)

    so far here is what i got

    int num;

    System.out.println ("Please enter a number: ");
    num = Integer.parseInt(input.readLine());

    while (num <= 10){
    num = 1*1+2*2+num*num;
    System.out.println("The answer is: "+ num);

    i know it only prohibits me to only 3 but thats it!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    What is your question?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    OK, I'll go first.

    The instruxctions say to add from 1 to num entered by user, so why does your loop go while num is less than 10?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Why use a loop at all? A bit of simple math shows that:

    Java Code:
    // sum of squares up to n:
    int sos(int n) { 
       return n*(n+1)*(2*n+1)/6; 
    }
    All those computer kids don't think anymore nowadays ... *grmbl*

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    kurap3ka is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Talking

    *facepalm

    JosAH u are the MAN!!!

    THANK YOU!!! :D

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kurap3ka View Post
    *facepalm

    JosAH u are the MAN!!!

    THANK YOU!!! :D
    Doesn't your assignment text say that you have to use a loop? My code snippet doesn't use loops ... Oh well, the cat's out the bag already:

    Java Code:
    int sos(int n) {
       int sum= 0;
       for (int i=1; i <= n; i++)
          sum+= i*i;
       return sum;
    }
    The above method does exactly the same as my previous little method. I prefer the first one over this silly loop version.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. adding numbers in an array together
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 07:23 PM
  2. Adding numbers for sales reciept program(Biginner)
    By waklo99 in forum New To Java
    Replies: 10
    Last Post: 03-18-2010, 04:09 AM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •