Thread: How to print This series?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Question How to print This series?

    Hello,

    Here I have a question that how to print the sum (answer) of the following series using loops in C:

    12+22+32+42+52+62+......+n2

    "n" is the given range of series by the user(input).
    Last edited by shansajid; 01-02-2013 at 06:14 AM. Reason: Missed something

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Are you aware of the for loop?
    Here is an example.
    Code:
    int i;
    int sum = 0;
    for(i = 0 ; i < 50 ; i++)
    {
          sum += i*5;
    }
    printf("sum is %d\n", sum);
    This will give the sum of
    5*1 + 5*2 + ... + n*5
    I think I already said too much
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm still trying to figure out the pattern. The fifth term is bugging me.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    I'm still trying to figure out the pattern. The fifth term is bugging me.
    Nice observation...

    shansajid did you miss a 4˛? If not, then you could easily bypass this term..but I think you missed it :P
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    oh sorry i missed. edited....

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by shansajid View Post
    oh sorry i missed. edited....
    No problem Whiteflags is too good to miss something ;p
    Have you come up with some code?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    std10093, Im trying this code that you shared but its output is wrong. Whats the matter with this code?

    Code:
     
    #include <stdio.h>
    #include <conio.h>
     
    void main()
    {
    clrscr();
    
           int a,n,sum;
           printf("\nEnter the range: ");
           scanf("%d", &n);
    
           for(a=0; a<=n; a++)
           {
        sum += a*a;
           }
           printf("%d", sum);
    
    
    getch();
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should initialise sum to 0 to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Take again a look in my example. There are two things that are different
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    your code shows that 5 is multiplied in all elements of a series. but i replaces usually 5 by the current element which is going to multiplied.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    yes sum=0, now its running correct

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Good move!

    But, as Salem already said, I have initialized a variable... Also my for loop is a little different
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by shansajid View Post
    yes sum=0, now its running correct
    Oh... Take a look at Salem's avatar (picture).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    Thankyou std10093 and Salem for your help

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Works for me - post your evidence.
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    int main()
    {
           int a,n,sum = 0;
           printf("\nEnter the range: ");
           scanf("%d", &n);
    
           for(a=0; a<=n; a++)
           {
        sum += a*a;
           }
           printf("%d\n", sum);
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    
    Enter the range: 5
    55
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  3. Help me with this series
    By NoUse in forum C Programming
    Replies: 6
    Last Post: 01-22-2009, 11:57 PM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM