Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calculate sum of M natural numbers starting from N.

Calculate sum of M natural numbers starting from N.

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13447042
    wrote on last edited by
    #1

    Calculate sum of M natural numbers starting from N, where N and M should be read from standard input (separated by space). User FOR statement.
    Example
    Input:
    3 4
    Output:
    18

    So my code is:
    #include int main(){
    int i,n,m,sum;
    scanf("%d %d",&n,&m);
    sum=n;
    for(i=0;i<=m;i++)
    sum=sum+i;
    printf("%d",sum);
    }
    I set i=0 because I figured since the sum is supposed to start from N itself then that's the way to do it. The problem is though it doesn't work the way it should. What's wrong with my code? :sigh:

    V CPalliniC 2 Replies Last reply
    0
    • U User 13447042

      Calculate sum of M natural numbers starting from N, where N and M should be read from standard input (separated by space). User FOR statement.
      Example
      Input:
      3 4
      Output:
      18

      So my code is:
      #include int main(){
      int i,n,m,sum;
      scanf("%d %d",&n,&m);
      sum=n;
      for(i=0;i<=m;i++)
      sum=sum+i;
      printf("%d",sum);
      }
      I set i=0 because I figured since the sum is supposed to start from N itself then that's the way to do it. The problem is though it doesn't work the way it should. What's wrong with my code? :sigh:

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Member 13478986 wrote:

      sum=n; for(i=0;i<=m;i++) sum=sum+i;

      1. you are trying to sum (M+1) natural numbers: starting with 0 and ending with M... 2. You are trying to sum not was required (i.e. N + N+1 + N+2 ... + N+M) but (N + 0 + 1 + ... + M)

      U 1 Reply Last reply
      0
      • V Victor Nijegorodov

        Member 13478986 wrote:

        sum=n; for(i=0;i<=m;i++) sum=sum+i;

        1. you are trying to sum (M+1) natural numbers: starting with 0 and ending with M... 2. You are trying to sum not was required (i.e. N + N+1 + N+2 ... + N+M) but (N + 0 + 1 + ... + M)

        U Offline
        U Offline
        User 13447042
        wrote on last edited by
        #3

        that helped a lot, i changed it to

        sum=0;
        for(i=0;i
        works as it should, thanks!

        1 Reply Last reply
        0
        • U User 13447042

          Calculate sum of M natural numbers starting from N, where N and M should be read from standard input (separated by space). User FOR statement.
          Example
          Input:
          3 4
          Output:
          18

          So my code is:
          #include int main(){
          int i,n,m,sum;
          scanf("%d %d",&n,&m);
          sum=n;
          for(i=0;i<=m;i++)
          sum=sum+i;
          printf("%d",sum);
          }
          I set i=0 because I figured since the sum is supposed to start from N itself then that's the way to do it. The problem is though it doesn't work the way it should. What's wrong with my code? :sigh:

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          You need no iteration, actually. Since (see 1 + 2 + 3 + 4 + ⋯ - Wikipedia[^])

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

          In your case

          SUM = ((N + M - 1) * (N + M) - (N - 1) * (N)) / 2

          Let's try the formula with the input

          N=3, M=4

          SUM = ((3 + 4 -1) * (3 + 4) - (3) * (2)) / 2 = (6 * 7 - 3 * 2) / 2 = (42 - 6) / 2 = 18

          In testa che avete, signor di Ceprano?

          U 1 Reply Last reply
          0
          • CPalliniC CPallini

            You need no iteration, actually. Since (see 1 + 2 + 3 + 4 + ⋯ - Wikipedia[^])

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

            In your case

            SUM = ((N + M - 1) * (N + M) - (N - 1) * (N)) / 2

            Let's try the formula with the input

            N=3, M=4

            SUM = ((3 + 4 -1) * (3 + 4) - (3) * (2)) / 2 = (6 * 7 - 3 * 2) / 2 = (42 - 6) / 2 = 18

            U Offline
            U Offline
            User 13447042
            wrote on last edited by
            #5

            yeah I mean it's just an arithmetic sequence so the easiest way would be to just use the formula. the thing is the task requires using the "for" loop so I had to.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups