# Sometimes doesnt work

#### faltu

Write a programme for estimation of the S value = n i=1 ∑ 1/ (ki) 2 + 1
Note: n as integer value and element values of k as float type should be taken from users

Sometimes when I give n the value 8 or a different value it asks for more than n values for k

my code:

#include <stdio.h>

int main()
{
int i,n;
float k,total;

printf("enter a value for n: ");
scanf("%d",&n);
total=0;
for(i=1;i<=n;i++)
{
for(k=i;k<=i;k++)
{
printf("enter values for k:");
scanf("%f",&k);
total=total+1/(k*k+1);

}
}

printf("result = %f\n",total);
return 0;

}

Last edited:

#### SBSPro725

You should define a new variable of type float and name it sum. Then replace the total=total+1/(k*k+1); with sum=sum+1/(k*k+1); . At last change the expression printf("result = %f\n",total); with printf("result = %f\n",sum); . Now it works fine if the values of n and k are not the same.

Here is the modified code:

C:
``````#include <stdio.h>
int main()
{
int i,n;
float k;
float sum;

printf("enter a value for n: ");
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
for(k=i;k<=i;k++)
{
printf("enter values for k:");
scanf("%f",&k);
sum=sum+1/(k*k+1);

}
}

printf("result = %f\n",sum);
return 0;

}``````

Hope that helps!