I want to calculate pi but I am getting 3.058403 as a result. How can I fix this code?
#include <stdio.h>
int main(){
double x = 0;
float g =0;
printf("How many terms to calculate pi to? ");
scanf("%f", &g);
int n;
for (n = 0; n < g ; n++){
double z = 1.0 / (2 * n + 1);
if ((n % 2) == 1){
z = z * -1;
}
x = (x + z);
}
double p = 4 * x;
printf("The value of pi is: %f", p);
return 0;
}
This is not really an answer because the actual answer has been discussed in the comment section.
Corrected version of your code:
removed pointless parentheses
use meaningful variable names
use of int for integer comparison
declaration of variables as close as possible to their scope
code formatted properly
#include <stdio.h>
int main() {
printf("How many terms to calculate pi to? ");
int nbofterms;
scanf("%d", &nbofterms);
double x = 0;
for (int n = 0; n < nbofterms; n++) {
double z = 1.0 / (2 * n + 1);
if (n % 2 == 1) {
z *= -1;
}
x = (x + z);
}
double pi = 4 * x;
printf("The value of pi is: %f", pi);
return 0;
}
Related
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
double sin1x(double converted,int i);
double cos1x(double converted,int i);
int main(){
int r,i = 1;
double converted,p,results,results1;
printf("Insert degrees from 0..2π: ");
scanf("%d", &r);
if(r < 0 || r > 360)
{
printf("NOT ACCEPTABLE DEGREES\n");
exit(0);
}
converted = r * PI / 180;
printf("Conversion from degrees to rad = %.3f", converted);
results = sin1x(converted,i);
results1 = cos1x(converted,i);
printf("\nsin(%d) = %.3f\n", r, results);
printf("\nsin(%d) of c = %.3f\n", r,sin(converted));
printf("\ncos(%d) = %.3f\n", r, results1);
printf("\ncos(%d) of c = %.3f\n", r,cos(converted));
return 0;
}
double sin1x(double x, int i)
{
int j = 3;
double sinx,numerator = x,pr;
sinx = numerator;
do
{
pr = numerator;
numerator = pr * x * x / (j * (j - 1));
if(i % 2 == 0)
sinx = sinx + numerator;
else
{
if(i % 2 == 1)
sinx = sinx - numerator;
}
i++;
j+=2;
}
while(fabs(pr - numerator) > 0.000001);
return sinx;
}
double cos1x(double x, int i)
{
int j = 2;
double cosx,numerator = x,pr;
cosx = numerator;
do
{
pr = numerator;
numerator = pr * x * x / (j * (j - 1));
if(i % 2 == 0)
cosx = cosx + numerator;
else
{
if(i % 2 == 1)
cosx = cosx - numerator;
}
i++;
}
while(fabs(pr - numerator) > 0.000001);
return cosx;
}
Hello I try to make a program with cosx and sinx and for some reason I have a problem with cosx. I cannot find any issues with my program but the cos results are wrong.Also I have the sin() and cos() functions to compare the results. I tried changing j or making another
variable to factorial but it didn't change anything.
At least these problems:
Wrong initialization
// double cosx,numerator = x,pr;
double cosx,numerator = 1.0,pr;
Missing change to j
// Add to `cos1x()` do loop
j += 2;
Coarse PI
No reason to not use a more precise value.
// #define PI 3.14159265
#define PI 3.1415926535897932384626433832795
Spelling
Convertion --> Conversion
Candidate simplification
double cos1x_alt(double x) {
double xx = x * x;
double term = 1.0;
double sum = term;
for (int i = 1; 1.0 + term != 1.0; i += 2) {
term *= -xx / (i * (i + 1));
sum += term;
}
return sum;
}
I made a code and do not understand how to force it work. I`m sure that problem is somewhere in loop but do not know how to fix it. Here is a task:
Compose a program for approximate calculation of the value of the function Y(x) in
points 0 < | x | < 1 using the Taylor series expansion of S(x). Find the approximate value of the function with an error less than ε < 0.0001. Enter the values of x and ε from the keyboard. Display the exact value of Y(x), the approximate value of S(x) found, and the resulting error | S(x) – Y(x) |.
There is my code:
`#include <stdio.h>
#include <math.h>
int main(){
double x, E;
printf("x: ");
scanf("%lf", &x);
printf("E: ");
scanf("%lf", &E);
double Yx = sin(x);
double Sx = 0;
if(fabs(x) >= 1 && E >= 1e-4)
printf("Change x and e");
else if(E >= 1e-4)
printf("Change e");
else if(fabs(x) >= 1)
printf("Change x");
else{
for(int k = 0;;k++) {
double f2 = 2 * k + 1;
for(int l = 1; l <= k; l++)
f2 = f2 * l;
double S2 = pow(-1, k) * (pow(x, (2 * k + 1)) / f2);
if(fabs(Sx-Yx) <= E)
break;
Sx= Sx + S2;
}
printf(" Y(x) = %lf\n", Yx);
printf(" S(x) = %lf\n", Sx);
printf(" |S(x)-Y(x)| = %lf \n", fabs(Yx-Sx));
return 0;
}
}`
This code does not compute the desired factorial correctly:
double f2 = 2 * k + 1;
for(int l = 1; l <= k; l++)
f2 = f2 * l;
In the future, step through your program in a debugger or insert printf statements to show the values of the variables. Printing the value of f2 after that loop would have revealed it is not the desired value.
Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........]
Test Data:
Input the Value of x :2
Input the number of terms : 5
Expected Output:
the sum = -0.415873
Number of terms = 5
Here is the code I wrote, no compilation error, I just wasnt getting the answer right:
#include <stdio.h>
#include <math.h>
int main()
{
float sum=0;
float ans;
int c, y, fac=1;
int a,i, x=2;
float z;
for (i = 1; i<=2; i++)
{
a= 2*i;
y = pow(2,a);
for (c = 1; c<=a; c++)
{
fac= fac*c;
}
z = (float) y/fac;
if (i%2 == 0) {
sum = sum + z;
}
else{
sum = sum - z;
}
}
ans = 1 + sum;
printf("The answer is %f" , ans);
return 0;
}
You didn't used number of terms.
You didn't set fac to 1 to the end of the for.
y is equal to pow(x,a) not pow(2,a).
Int can store numbers between -2,147,483,648 and 2,147,483,647, so I recommend to use long long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). And also, double instead of float. You can take a look here for more details: https://learn.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-160.
Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
double sum=0;
double ans;
long long c, y, fac=1;
long long a,i, x=2, numberOfTerms = 5;
float z;
for (i = 1; i<= numberOfTerms; i++)
{
a= 2*i;
y = pow(x,a);
for (c = 1; c<=a; c++)
{
fac= fac*c;
}
z = (double) y/fac;
if (i%2 == 0) {
sum = sum + z;
}
else{
sum = sum - z;
}
fac = 1;
}
ans = 1 + sum;
printf("The answer is %f" , ans);
return 0;
}
Try to use functions.
If you do not have to, do not use float. Use double for floating point calculations.
Your code does not reflect the formula at all.
double fact(unsigned n)
{
double result = 1.0;
for(unsigned x = 1; x <= n; x++)
result *= x;
return result;
}
double series(double x, unsigned nterms)
{
double result = 1;
for(unsigned term = 1; term <= nterms; term++)
{
result += (1.0 - ((term & 1) << 1)) * pow(x, (double)term * 2.0) / fact(term * 2);
}
return result;
}
int main(void)
{
for(unsigned nterms = 2; nterms < 20; nterms++)
{
printf("nterms = %2u series(4) = %.32f\n", nterms, series(4.0, nterms));
}
}
https://godbolt.org/z/8c3xM4
Does anybody have any idea what this code does? I suspect it has something to do with Taylor series, but I'm not sure since I don't really know what Taylor series is. However, it could be anything. I'm really not sure.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
double x, y, a;
int n;
int z;
x = 25.0;
if (z < 0) {
z = 1;
x = -x;
} else
z = 0;
n = 0;
a = 1;
y = 1;
for (n = n + 1; n < 20; n++) {
a = a * x / n;
y = y + a;
printf("%i \t %.20g \t %g \n", n, y, a);
}
if (z) {
x = -x;
y = 1.0 / y;
}
printf("%i \t %.20g \t %g \n", n, y, a);
return 0;
}
Enable compiler warnings.
if (z < 0) { is a coding error. #Pablo.
It certainly should be
if (x < 0) {
Unrolling
n = 0;
a = 1;
y = 1;
for (n = n + 1; n < 20; n++) {
a = a * x / n;
y = y + a;
The terms are
y = 1 + x/1 + x*x/(1*2) + x*x*x/(1*2*3) + .... pow(x,20)/(1*2*3*...*20)
Review Taylor series to discern which one.
BTW, For x==25, code Taylor's series does not converge enough. Either more terms are needed or better - an alternate/additional approach is needed.
good day, I am making a program that is using the e function:
e(n) = 1 + (1/1!) + (1/2!)+ ... + (1/n!)
with this function of C:
double e(double x, double x2){
double sum, kf;
int i, m, n;
// printf("Please input the number of x: ");
// scanf("%lf", &x);
sum = 0.0;
for (i=1; i<=x; i++){
kf=1.0;
for (m=1; m<=i; m++) {
kf*=1.0/m;
}
sum+=kf;
}
return printf("e=%lf\n", 1+sum);
}
well, it was the e function. now, I want to find the minimum number n that makes:
|e(n) - e(n+1)| < x
(absolute value of e(n) - e(n+1) is smaller than x
where x is a long float that users input, will be 0.1, 0.001, 0.0001,...
Any answers are highly appreciated.
Using algebra and the e function definition, |e(n) - e(n+1)| < x can be simplified to (1/n!) < x, which can be expanded to (1/1) * (1/2) * (1/3) * ... * (1/n) < x.
int minN(double x) {
double sum = 1;
int n;
for(n = 0; sum >= x; n++) {
sum *= 1/(n+1);
}
return n;
}