Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........]
Test Data:
Input the Value of x :2
Input the number of terms : 5
Expected Output:
the sum = -0.415873
Number of terms = 5
Here is the code I wrote, no compilation error, I just wasnt getting the answer right:
#include <stdio.h>
#include <math.h>
int main()
{
float sum=0;
float ans;
int c, y, fac=1;
int a,i, x=2;
float z;
for (i = 1; i<=2; i++)
{
a= 2*i;
y = pow(2,a);
for (c = 1; c<=a; c++)
{
fac= fac*c;
}
z = (float) y/fac;
if (i%2 == 0) {
sum = sum + z;
}
else{
sum = sum - z;
}
}
ans = 1 + sum;
printf("The answer is %f" , ans);
return 0;
}
You didn't used number of terms.
You didn't set fac to 1 to the end of the for.
y is equal to pow(x,a) not pow(2,a).
Int can store numbers between -2,147,483,648 and 2,147,483,647, so I recommend to use long long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). And also, double instead of float. You can take a look here for more details: https://learn.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-160.
Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
double sum=0;
double ans;
long long c, y, fac=1;
long long a,i, x=2, numberOfTerms = 5;
float z;
for (i = 1; i<= numberOfTerms; i++)
{
a= 2*i;
y = pow(x,a);
for (c = 1; c<=a; c++)
{
fac= fac*c;
}
z = (double) y/fac;
if (i%2 == 0) {
sum = sum + z;
}
else{
sum = sum - z;
}
fac = 1;
}
ans = 1 + sum;
printf("The answer is %f" , ans);
return 0;
}
Try to use functions.
If you do not have to, do not use float. Use double for floating point calculations.
Your code does not reflect the formula at all.
double fact(unsigned n)
{
double result = 1.0;
for(unsigned x = 1; x <= n; x++)
result *= x;
return result;
}
double series(double x, unsigned nterms)
{
double result = 1;
for(unsigned term = 1; term <= nterms; term++)
{
result += (1.0 - ((term & 1) << 1)) * pow(x, (double)term * 2.0) / fact(term * 2);
}
return result;
}
int main(void)
{
for(unsigned nterms = 2; nterms < 20; nterms++)
{
printf("nterms = %2u series(4) = %.32f\n", nterms, series(4.0, nterms));
}
}
https://godbolt.org/z/8c3xM4
Related
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
double sin1x(double converted,int i);
double cos1x(double converted,int i);
int main(){
int r,i = 1;
double converted,p,results,results1;
printf("Insert degrees from 0..2π: ");
scanf("%d", &r);
if(r < 0 || r > 360)
{
printf("NOT ACCEPTABLE DEGREES\n");
exit(0);
}
converted = r * PI / 180;
printf("Conversion from degrees to rad = %.3f", converted);
results = sin1x(converted,i);
results1 = cos1x(converted,i);
printf("\nsin(%d) = %.3f\n", r, results);
printf("\nsin(%d) of c = %.3f\n", r,sin(converted));
printf("\ncos(%d) = %.3f\n", r, results1);
printf("\ncos(%d) of c = %.3f\n", r,cos(converted));
return 0;
}
double sin1x(double x, int i)
{
int j = 3;
double sinx,numerator = x,pr;
sinx = numerator;
do
{
pr = numerator;
numerator = pr * x * x / (j * (j - 1));
if(i % 2 == 0)
sinx = sinx + numerator;
else
{
if(i % 2 == 1)
sinx = sinx - numerator;
}
i++;
j+=2;
}
while(fabs(pr - numerator) > 0.000001);
return sinx;
}
double cos1x(double x, int i)
{
int j = 2;
double cosx,numerator = x,pr;
cosx = numerator;
do
{
pr = numerator;
numerator = pr * x * x / (j * (j - 1));
if(i % 2 == 0)
cosx = cosx + numerator;
else
{
if(i % 2 == 1)
cosx = cosx - numerator;
}
i++;
}
while(fabs(pr - numerator) > 0.000001);
return cosx;
}
Hello I try to make a program with cosx and sinx and for some reason I have a problem with cosx. I cannot find any issues with my program but the cos results are wrong.Also I have the sin() and cos() functions to compare the results. I tried changing j or making another
variable to factorial but it didn't change anything.
At least these problems:
Wrong initialization
// double cosx,numerator = x,pr;
double cosx,numerator = 1.0,pr;
Missing change to j
// Add to `cos1x()` do loop
j += 2;
Coarse PI
No reason to not use a more precise value.
// #define PI 3.14159265
#define PI 3.1415926535897932384626433832795
Spelling
Convertion --> Conversion
Candidate simplification
double cos1x_alt(double x) {
double xx = x * x;
double term = 1.0;
double sum = term;
for (int i = 1; 1.0 + term != 1.0; i += 2) {
term *= -xx / (i * (i + 1));
sum += term;
}
return sum;
}
Following needs to be calculated (iterative und recursive Solution)
Sum = 1 – 1/2 + 1/3 – 1/4 + - . . . 1/n (n > 0) with float reihe (int n)
We have the following so far:
#include <stdio.h>
float reihe(int n)
{
float sum = 0;
if (n = 1)
sum = 1;
else
{
for (int i = 1; i < n; i++)
{
sum += (1 / i) - (1 / (1 + i));
}
}
return sum;
}
int main(void)
{
float z;
z = reihe(5);
printf("%d", z);
return 0;
}
Would appreciate any of your help. Have a good day.
What about something like this? Not tested, but it compiles and runs...
BTW/ take a close look at the use of 1.0. If you use 1 it is interpreted as an integer and not a float and you division is always rounded to 1 or 0 and you lose all decimal values...
float reihe(int n)
{
float sum = 0;
float base = 1.0;
for (int i = 1; i <= n; i++) {
if ( i % 2 ) {
sum += base/i;
}
else {
sum -= base/i;
}
}
return sum;
}
double taylor_log(double x, unsigned int n){
double tmp;
double sum = 0;
if(x < 1){
int j = 2;
x = 1 - x;
tmp = x;
sum = -x;
for(unsigned int i = 1; i < n; i++){
sum -= ((tmp *= x) / j);
j++;
}
return sum;
}
else if (x >= 1){
tmp = ((x-1)/x);
for(unsigned int i = 1; i <= n; i++){
sum += (tmp/i);
tmp *= ((x-1)/x);
}
return sum;
this is my fuction for log with taylor series wich work correctly.
Im using this formula to get exponetional fuction of number.formula for mypow()
and this is my code for pow
double taylor_pow(double x, double y, unsigned int n){
double sum = 1.0;
int fac = 1;
double exp = y;
double lna = taylor_log( x, n);
for(unsigned int i = 1; i <= n; i++){
fac *= i;
sum += (exp * lna / fac );
exp *= y;
lna *= taylor_log( x, n);
}
return sum;
}
Now my problem is that if i put for example 30 iterations for my function the number is higher than pow(). For example pow(2,3) = 8 and my result with 20 iterations is 8.0007... and its growing. Thans for all responses.
int fac is overflowing. Changed it to long double, and it works much better.
A 32 bit signed int will only hold values up to 12!, while an 80 or 128 bit long double can hold something like 2000!.
I am trying to input the value of x and a value for n for the number of terms to find the natural log using Taylor series and another series. The problem is is that my output is not showing up but just showing a blank space when I enter values. Please help!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int argc, char **argv){
double x = atof(argv[1]);
double i;
double y;
double result2;
double result;
double error1;
double error2;
double sum;
int n = atof(argv[2]);
if( x <= 0){
printf("Invalid argument\n");
exit (1);
}
if(abs(x-1) <= 1 && abs(x-1) !=0){
for (i = 1; i <= 1; i++){
result -= pow((x-1), i )/ i;
}
}
else{
for(i =1; i <=n; i--){
result += 1/(i * pow((y),i));
}
}
for(i = 0; i <=n; i+=2){
y = (x-1)/(x+1);
sum += pow((y),i) * (1 / (1+i));
result2 = sum * 2 * y;
}
error1 = result - log(x);
error2 = result2 - log(x);
printf("Taylor series: ln(%lf) ~= %lf\n", x, result);
printf(" Error: %lf\n", error1);
printf("Other series: ln(%lf) ~= %lf\n", x, result2);
printf(" Error: %lf\n", error2);
return 0;
}
You are using y without initializing it. Initialize it first before using it in the program. Specifically this line
result += 1/(i * pow((y),i));
will try to use y when it is null.
Also the loop
for (i = 1; i <= 1; i++)
is not that effective as it is iterating only once.
My question is how do I compute 2^(x) in c. I know there is something like shifting that does the same thing. I tried to do total = x << 1, but that didn't work. I know that if i shift one bit it is the same as multiplying it by two. Or something like that.
int x;
for(x=0; x<4; x++){
total += x <<1; //
}
When this is done executing I expect the total to be 15 ( 20 + 21 + 22 + 23 )
Any ideas on what I am doing wrong? my total starts off as being 0 and then messes up.
Thanks!
It's the other way around. 1 << x will give you '2 ^ x'.
This should do what you want. Call pow(2, x) to get 2x.
int abs (int x) {
if (x < 0) return -x;
return x;
}
int signum (int x) {
if (x < 0) return -1;
if (x > 0) return 1;
return 0;
}
int add (int x, int y) {
for (int i = 0; i < abs(y); ++i) {
if (y > 0) ++x;
else --x;
}
return x;
}
int mult (int x, int y) {
int sign = signum(x) * signum(y);
x = abs(x);
y = abs(y);
int res = 0;
for (int i = 0; i < y; ++i) {
res = add(res, x);
}
return sign * res;
}
int pow (int x, int y) {
if (y < 0) return 0;
int res = 1;
for (int i = 0; i < y; ++i) {
res = mult(res, x);
}
return res;
}
Left shift is limited to the word size of your CPU, either 32 or 64 bits, which limits the maximum exponent which you can safely use, before the result is undefined (2^31 or 2^63).
The following works for larger exponents but uses floating point arithmetic. If you need exact results you should consider using a infinite precision maths library such as GMP
#include <math.h>
int main() {
double base = 2;
double exponent = 4;
double result = pow(base, exponent);
return 0;
}