This blog is under construction

Saturday 22 June 2013

C program to evaluate the series 1 + 2*1 + 3*2 + 4*3 + 5*4 + ... N*N-1

Write a C program to evaluate the series 1 + 2*1 + 3*2 + 4*3 + 5*4 + ... N*N-1.



  #include <stdio.h>
  int main () {
        int temp, n, i, res = 1;
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* evaulate the series 1 + 2*1 + 3*2 + 4*3 + ... N*N-1 */
        for (i  = 2; i <= n; i++) {
                res = res + (i * (i - 1));
        }

        printf("Output: %d\n", res);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out 
  Enter the value for n:10
  Output: 331



No comments:

Post a Comment