-6

1.2^2+2.3^2+3.4^2+4.5^2+ need to calculate the sum up-to nth number with C programming. But I can't find any way to solve the program.

When the user in put 4 as the value of n, the sum will be the total of 1.2^2+2.3^2+3.4^2+4.5^2.

Can anyone help me get the algorithm?

8
  • 1
    Do you know how to set up a for loop? Also, presumably you mean a power series, ^ is XOR in C. But, check there's not a closed-form solution to this first.
    – Bathsheba
    Mar 27, 2015 at 15:33
  • I try with this total=pow((n*(n+1)/2),2) + 2*(n*(n+1)*(2*n+1))/6 + n*(n+1)/2 but I don't know, why output is wrong. I got this law from internet
    – Sudarshan
    Mar 27, 2015 at 15:36
  • I tried with this. {#include<stdio.h> main() { int n; scanf("%d,",&n); //pow(n,2)*pow((n+1),2)/4 +n*(n+1)*(2*n+1)/3 + n*(n+1)/2; int total=pow((n*(n+1)/2),2) + 2*(n*(n+1)*(2*n+1))/6 + n*(n+1)/2; printf("%d",total); } }
    – Sudarshan
    Mar 27, 2015 at 15:38
  • 2
    @Sudarshan Please edit your post and put your attempts inside code formatting so we can actually read it. Also, posting your attempts so far, even if they aren't working, is a good way to avoid down votes for lack of research effort.
    – Lundin
    Mar 27, 2015 at 15:41
  • 1
    a = n*(n-1)*(n+1)*(3*n+2)/12
    – r3mainer
    Mar 27, 2015 at 15:43

2 Answers 2

1

A simple for loop would do it:

int compute(int n) {   
  int i, sum=0;

  for(i=1; i<=n; i++) {
     int val = i*(i+1)*(i+1);
     sum += val;
  } 
  return sum;
}
7
  • Please give me fish.
    – Lundin
    Mar 27, 2015 at 15:39
  • The expression int val = i*(i+1)*(i+1); seems incorrect.
    – chqrlie
    Mar 27, 2015 at 16:12
  • 1
    @chqrlie It seems from the author's question and comments, that the "." is used to denote multiplication and not the decimal point in his intended summation.
    – sray
    Mar 27, 2015 at 16:34
  • Trivial edit so I can reverse my downvote. This answer is correct, if inefficient.
    – Bathsheba
    Mar 27, 2015 at 16:40
  • Ooops! you are correct. I did suspect the question to be misformulated. The OP wants to compute the sum of cubes minus the sum of squares. Direct answer is: return n*n*(n+1)*(n+1)/4 - n*(n+1)(2*n+1)/6; which can be further simplified.
    – chqrlie
    Mar 28, 2015 at 17:15
1
  • for(int i=0; i<n; i++). This is a for loop.
  • Inside the loop, store the loop iterator i in a double variable.
  • Add 1.2 to it.
  • Multiply it by itself.
  • Do something with the result: print it, and/or add it to a sum variable etc.
  • Do not use the xor operator ^ for this.
9
  • 1
    +1 for teaching someone how to fish. (But do note there is a closed-form solution for this particular series).
    – Bathsheba
    Mar 27, 2015 at 15:41
  • The OP's question is not stated clearly, but I doubt your advice addresses the problem. The sequence seems to be: 1.2, 2.3, 3.4, 5.4... not 1.2, 2.2, 3.2... But the actual question might be much simpler: sum of consecutive squares.
    – chqrlie
    Mar 27, 2015 at 16:11
  • Why not for (float i = 1.2f; i < n+1; i+=1.1f)?
    – EOF
    Mar 27, 2015 at 16:16
  • More something like: double x, sum = 0.0; int i; for (i = 0, x = 1.2; i < n; i++, x += 1.1) sum += x * x;
    – chqrlie
    Mar 27, 2015 at 16:25
  • @chqrlie: This again duplicates the calculations, and is less readable. The only way it could be faster is if floating-point comparisons are very expensive.
    – EOF
    Mar 27, 2015 at 23:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.