U
Usman
Huy everyone ,
Well I am not a big C++ programmer , I am just a little
young kid on it tryint to learn . Actually I was given an
assignment last week by my teacher which I solved
completely but was unable to go through one question.
I did solved it but I myself wasn't satisfactory. I am sending in
the question and my program code for it. I hope some one
can guide me onto it.
Bye.
Yours forever in Digital Paradise.
uSmAn
HERE GOES THE QUESTION :
The value ex can be approximated by the sum:
1 + x + x2/2! + x3/3! + . + xn/n!
Write a program that takes a value x as input and outputs the
above sum for n taken to be each of the values 1 to 100. To
compare the value calculated by your program and the exact value,
your program should also output ex calculated using the standard function
exp.
HERE IS MY CODE FOR IT : I was unable to store value for a factorial
of 100 so I deducted my loop to 34.
#include<iostream.h>
#include<conio.h>
#include<math.h>
unsigned long factorial ( unsigned long );
int main()
{
char ch;
do
{
clrscr();
unsigned long x,n,result=1;
unsigned long exres;
cout << " Enter a Number : " ;
cin >> x ;
cout << endl << endl;
result = 1 + x;
for ( n = 1; n <= 33 ; n++ )
{
result += ( x ^ n ) / factorial(n);
}
exres = exp(x);
cout << " Your result by programming formula is : " << result ;
cout << endl << endl ;
cout << " The exact value of the exp. func is : " << exres;
cout << endl ;
cout << "\n Want to perform again ? (y/n) ";
cin >> ch;
}
while ( ch != 'n' );
return 0;
}
unsigned long factorial( unsigned long facto )
{
if ( facto <= 1 )
return 1;
else
return facto * factorial(facto -1);
}
Well I am not a big C++ programmer , I am just a little
young kid on it tryint to learn . Actually I was given an
assignment last week by my teacher which I solved
completely but was unable to go through one question.
I did solved it but I myself wasn't satisfactory. I am sending in
the question and my program code for it. I hope some one
can guide me onto it.
Bye.
Yours forever in Digital Paradise.
uSmAn
HERE GOES THE QUESTION :
The value ex can be approximated by the sum:
1 + x + x2/2! + x3/3! + . + xn/n!
Write a program that takes a value x as input and outputs the
above sum for n taken to be each of the values 1 to 100. To
compare the value calculated by your program and the exact value,
your program should also output ex calculated using the standard function
exp.
HERE IS MY CODE FOR IT : I was unable to store value for a factorial
of 100 so I deducted my loop to 34.
#include<iostream.h>
#include<conio.h>
#include<math.h>
unsigned long factorial ( unsigned long );
int main()
{
char ch;
do
{
clrscr();
unsigned long x,n,result=1;
unsigned long exres;
cout << " Enter a Number : " ;
cin >> x ;
cout << endl << endl;
result = 1 + x;
for ( n = 1; n <= 33 ; n++ )
{
result += ( x ^ n ) / factorial(n);
}
exres = exp(x);
cout << " Your result by programming formula is : " << result ;
cout << endl << endl ;
cout << " The exact value of the exp. func is : " << exres;
cout << endl ;
cout << "\n Want to perform again ? (y/n) ";
cin >> ch;
}
while ( ch != 'n' );
return 0;
}
unsigned long factorial( unsigned long facto )
{
if ( facto <= 1 )
return 1;
else
return facto * factorial(facto -1);
}