Efficient Programming

A place where you can share ideas to do a better programming. A better code. A better world :)

Friday, July 11, 2008

Find the sum of a series. S = 1-3+5-7+9 ... n

/*in c language */
int s=0;
int sign=1;
int i;
for(i=1; i<=n; i+=2)
{
s = s+ sign*n;
sign = -sign;
}

/*s will contain the sum of the series*/
Posted by Rahul Bhawsar at 4:43 AM

No comments:

Post a Comment

Newer Post Home
Subscribe to: Post Comments (Atom)

Pages

  • Data Structure & Algorithms
  • Java

Blog Archive

  • ►  2014 (1)
    • ►  May (1)
  • ►  2013 (1)
    • ►  August (1)
  • ▼  2008 (7)
    • ▼  July (7)
      • Palindrome
      • To reverse a string.
      • A program to move a circle in a box.
      • how to find HCF of two or more numbers.
      • Is there any efficient way to calulate the on bits...
      • to find whether a number is in power of 2 or not.
      • Find the sum of a series. S = 1-3+5-7+9 ... n
Simple theme. Powered by Blogger.