Please Debug this C++ program

Status
Not open for further replies.

ajaybc

Youngling
I made this program for printing the sine series.It is not displaying any errors but just exiting without showing the result.Please help...
#include<iostream.h>
#include<conio.h>
#include<math.h>

double fact(double n)
{
if(n!=1)
return n*(n-1);
}

void main()
{
clrscr();
double f,x,s=-1,j,i,n;
cout<<"Enter the number:";
cin>>x;
cout<<"Upto how many terms:";
cin>>n;
j=1;
for(i=1;i<=n;i++)
{
s=s*(-1);
f=fact(j);
x=x+(s*(pow(x,j)/f));
j=j+2;
}
cout<<"Answer:"<<x;
getch();
}
 

damngoodman999

damnbadman666
Stack level problem , take out the DOUBLE
-----------------------------------------
Posted again:
-----------------------------------------
Floating point error ---- INTEGER is used for clearence and output
 
Last edited:

Liverpool_fan

Sami Hyypiä, LFC legend
Your factorial algorithm is wrong unless I am totally misunderstanding your code. And you don't use floating types in factorials. The factorials are always computed with integers AFAIK.

Code:
double fact(double n)
{
if(n!=1)
return n*(n-1);
}
This should be rather:

Code:
long int fact(int n)
{
if(n>1)
    return n*fact(n-1);
else
    return 1;
}

Maybe this will help.
Also a kindly advice. I suggest tou learn to indent your code. It will drastically improve readability.
 
Status
Not open for further replies.
Top Bottom