Sometimes doesnt work


Joined
Nov 5, 2022
Messages
2
Reaction score
0
aaa.png



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:
Ad

Advertisements

Joined
Nov 6, 2022
Messages
1
Reaction score
0
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top