Results 1 to 8 of 8

Math Help - C++ code?

  1. #1
    Junior Member
    Joined
    Jun 2008
    Posts
    53

    C++ code?

    For a homework problem I need to use a code to compute the sum of the integers from 1 to 5,000

    can someone help me or direct me to another site that can? Thanks.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Santa Cruz, CA
    Posts
    2,844
    Thanks
    3
    Quote Originally Posted by amor_vincit_omnia View Post
    For a homework problem I need to use a code to compute the sum of the integers from 1 to 5,000

    can someone help me or direct me to another site that can? Thanks.
    I know that C++ and Java are similar. I can give you the code to the program what you are looking for in Java, but you will need to make the proper conversion over to C++.

    Code:
    public class sum{
    
         public static void main(String [] args){
                
               // This part computes the sum 1+2+3+...+4999+5000
    
               int sum=0, cntr;
               for (cntr=1; cntr <= 5000; cntr++)
                    sum += cntr;
               System.out.println (sum);
         }
    
    }
    I hope this somewhat helps.

    --Chris
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Jul 2008
    Posts
    78
    I havin't programmed in awhile, but if my memory serves me correctly, it should be something like this:

    Code:
    int sum = 0;
    for(int i = 1; i >= 5000; i++)
    {
         sum += i;
    }
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    Jun 2008
    Posts
    53
    Thanks to both of you guys. Chris- I don't know Java so I'm not sure how to convert? I wish we could use Matlab or Fortran.

    It's been years since I've done any programming...thanks pnoy that's similar to mine. Anyone else feel free to post any ideas.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    o_O
    o_O is offline
    Primero Espada
    o_O's Avatar
    Joined
    Mar 2008
    From
    Canada
    Posts
    1,407
    Alternatively, we can use the fact that: 1 + 2 + \hdots + n = \frac{n(n+1)}{2}. This way, we don't have to use a loop that loops 5000 times.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Santa Cruz, CA
    Posts
    2,844
    Thanks
    3
    Quote Originally Posted by amor_vincit_omnia View Post
    Thanks to both of you guys. Chris- I don't know Java so I'm not sure how to convert? I wish we could use Matlab or Fortran.

    It's been years since I've done any programming...thanks pnoy that's similar to mine. Anyone else feel free to post any ideas.
    This may help. They compute the sum of the first 30 squares. Just change the code a little bit and you should get what you're looking for.

    --Chris
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Santa Cruz, CA
    Posts
    2,844
    Thanks
    3
    Quote Originally Posted by o_O View Post
    Alternatively, we can use the fact that: 1 + 2 + \hdots + n = \frac{n(n+1)}{2}. This way, we don't have to use a loop that loops 5000 times.
    Why didn't I think of that?

    --Chris
    Follow Math Help Forum on Facebook and Google+

  8. #8
    MHF Contributor arbolis's Avatar
    Joined
    Apr 2008
    From
    Teyateyaneng
    Posts
    1,000
    Awards
    1
    I did it in Fortran 90. For fortran 77, just put all in capital letters.
    "Program notfor
    implicit none

    Integer :: n
    Real :: z

    Write(*,*)'Enter a number'
    Read(*,*)n

    z=(n*(n+1))/2

    Write(*,*)z

    end program
    "
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. R-code help!
    Posted in the Math Software Forum
    Replies: 0
    Last Post: November 11th 2011, 09:01 AM
  2. Code
    Posted in the Algebra Forum
    Replies: 1
    Last Post: October 9th 2011, 01:08 AM
  3. Code.
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: March 19th 2011, 09:51 AM
  4. Code help
    Posted in the Discrete Math Forum
    Replies: 0
    Last Post: November 24th 2009, 09:42 AM
  5. Need help with R code
    Posted in the Math Software Forum
    Replies: 2
    Last Post: September 9th 2009, 02:10 PM

Search Tags


/mathhelpforum @mathhelpforum