DIGIT-ARC read-only archive · snapshot: July 23, 2026

Please Debug this C++ program

5 posts · originally from https://geek.digit.in/community/threads/please-debug-this-c-program.111306/

ajaybc
ajaybc Original post #1
Mar 21, 2009 at 2:06 PM
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
Mar 21, 2009 at 2:12 PM
Stack level problem , take out the DOUBLE
-----------------------------------------
Posted again:
-----------------------------------------
Floating point error ---- INTEGER is used for clearence and output
Liverpool_fan
Mar 21, 2009 at 3:48 PM
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.
ajaybc
ajaybc #4
Mar 21, 2009 at 5:56 PM
^^Thanks.Worked great.
QwertyManiac
Mar 22, 2009 at 6:57 PM
Next up, one liners. Same thing.
Code:
long int fact(int n) { return n>1?n*fact(n-1):1; }