[英]How to print this series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+…(x^n/n!) in C/C++?
I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.我想写一个程序来计算系列 x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)通过将 x 和 n 作为用户输入。
This is what i've tried, and well there's no output when I enter the values for x,n:这是我尝试过的,当我输入 x,n 的值时没有输出:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.当我输入 x 和 n 的值时,它只是不显示任何内容。 While I've written in C, C++ solutions are also appreciated.
虽然我是用 C 编写的,但也很欣赏 C++ 解决方案。
The loop循环
for(i=1;i<=t;i+2)
in the function series()
is an infinite loop when t >= 1
because i
isn't updated in the loop.当
t >= 1
时,函数series()
是一个无限循环,因为i
没有在循环中更新。 Try changing +
to +=
and use尝试将
+
更改为+=
并使用
for(i=1;i<=t;i+=2)
instead.相反。 Also it seems you should use type
int
for x
and n
in the function main()
because the arguments of series()
is int
.此外,您似乎应该在函数
main()
为x
和n
使用int
类型,因为series()
的参数是int
。 Don't forget to change the format specifier when changing their types.更改类型时不要忘记更改格式说明符。
Thanks to all those who helped.感谢所有帮助过的人。 Here's the final working code:
这是最终的工作代码:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) ie for factorial and multiply with -1 ie to achive negavtive number alternatively.在循环中,我们一步一步来获得奇数。通过将循环中的当前临时变量乘以循环中的前一个临时变量,并使用必要的术语,例如 x 平方,然后除以 i*(i-1) 即为阶乘并乘以 -1 即或者达到负数。 by using this temp variable and adding it to sum variable in every iteration will give us answer.
通过使用这个临时变量并将其添加到每次迭代中的 sum 变量会给我们答案。
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", °);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.