Error in Sum of conditional fractions

Asked

Viewed 22 times

0

I can’t do this exercise by using it, you could help me. I can not find where I am missing, I put two && no for to carry out the two operations but it is not compiling, thanks.

Make a program in C that calculates and prints the sum expressed by the following series:

S = 500/2 + 480/3 + 460/4 + ....

#include <stdio.h>

int main (){
int x, y;
float z;
x = 2;
y = 500;
z = 0;
for (;x = 20 %% y = 26; x = x - 20 && y = y + 1)
    z = z + x/y;

printf("%f", z);


return 0;
}
  • You are splitting integers (x / y). Use a float cast: z += (float) x / y;. Also it is && and not %%. At the end of the for use only ,: x -= 20 , y++.

  • Simpler would be: for (x = 500, y = 2; x >= 20 && y <= 26; x -= 20 , y++)

  • Thanks for the personal answers, worse that keeps giving error..

  • See: http://ideone.com/x1CQB1

  • But there you know the exact value of plots, I do not know the number of plots

  • So what is the condition for you to stop adding up?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.