Please help me with this C program -loop?

I really do not understand the following.It’s difficult to apply a google search to get a preview also.To the following issue my application is allowing zero for the reason that answer for just about any value I input.May anyone make sure you explain this

Thank you in advance

Problem,

Write some sort of C course to compute Summation from the following String.User should input benefit for d.
1+1/22+1/32+….+1/n2

It’s difficult that will type this above set.So Relating to uploaded this image

http://imageshack.us/photo/my-images/218/fractions.jpg/

My personal program,

#include
int my spouse and i, j, nited kingdom;
drift sum;
void main()
printf(“Enter the value pertaining to n:”, j);
scanf(“%d”, &j);
printf(“Sum with 1+1/22+1/32+….+1/n2 will be:%f”, sum);
for(i=1; i<=j; i++)
k=1/i*i;
sum+=k;

include
int my spouse and i, j, nited kingdom;
drift sum;
many problems:

int main(void)
printf(“Enter the value pertaining to n:”, j);
scanf(“%d”, &j);
printf(“Sum with 1+1/22+1/32+….+1/n2 will be:%f”, sum);
bzzzt———————————
sum does not have a value nevertheless.
for(i=1; i<=j; i++)
k=1/i*i;
bzzzt this is certainly (1/i)*i not necessarily what an individual want
use 1/i/i and also 1/(i*i)
bzzzt k is usually an int make use of float instead

sum+=k;
bzzt — sum seriously isn’t given a value

untested code follows:

#include
int j;
drift k, my spouse and i;
drift sum;
int main(void)
printf(“Enter the value pertaining to n:”, j);
scanf(“%d”, &j);
sum=0.0;

for(i=1; i<=j; i++)
k=1./i/i;
sum+=k;

printf(“Sum with 1+1/22+1/32+….+1/n2 will be:%f”, sum);
go back 0;

Your challenge has to do with your typecasting.You need a float answer but you’re implementing int
case:
int the = ONLY TWO;
int b = A FEW;
drift c = a/b;

envisioned output:0.4
genuine output:0

For the reason that integers just like rounding straight down, it kind of sucks that way.You ought to convert these to floats

drift sum = 0;
intended for (int my spouse and i = 1; i <=j; i++)

sum += 1/(((float) i) * ((float) i)));

Also an individual never initialized k that may lead that will problems.Truly k wasn’t even necessary.Still though should you be going to be incrementing anything, you ought to initialize the item first.

The greatest thing is that you are printing sum when you calculate this…
Roger seriously isn’t 100% correct, you merely have to force the effect of the calculation to suspended point i.e.

nited kingdom = ONE.0f / (float)(i*i);

.the first printf isn’t going to need j.

Leave a Reply