简体   繁体   中英

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.

This is what i've tried, and well there's no output when I enter the values for 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. While I've written in C, C++ solutions are also appreciated.

Output window in code::blocks

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. 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 . 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. by using this temp variable and adding it to sum variable in every iteration will give us answer.

#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", &deg);
  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);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM