If - Else Statement :
Syntax :
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
Problem :
A program to evaluate the power series.
e^x= 1 + x + x^2/2! + x^2/3! + ⋯ + x^n/n! , 0 < x < 1
Program Code :
#define ACCURACY 0.0001
main()
{
int n, count;
float x, term, sum;
printf(“Enter value of x:”);
scanf(“%f”, &x);
n=term=sum=count=l;
while (n <= 100)
{
term = term * x/n;
sum = sum + term;
count = count + 1;
if (term < ACCURACY)
n= 999;
else
n = n + 1;
}
printf(“Terms = %d SUM = %f \n”, count, sum);
}
Output :
Enter value of x:0
Terms = 2 sum = 1.000000
Enter value of x:0.1
Terms = 5 sum = 1.105171
Enter value of x:0.5
Terms = 7 sum = 1.648720
Enter value of x:0.75
Terms = 8 sum = 2.116997
Enter value of x:0.99
Terms = 9 sum = 2.691232
Enter value of x:1
Terms = 9 sum = 2.718279
Syntax :
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
Problem :
A program to evaluate the power series.
e^x= 1 + x + x^2/2! + x^2/3! + ⋯ + x^n/n! , 0 < x < 1
Program Code :
#define ACCURACY 0.0001
main()
{
int n, count;
float x, term, sum;
printf(“Enter value of x:”);
scanf(“%f”, &x);
n=term=sum=count=l;
while (n <= 100)
{
term = term * x/n;
sum = sum + term;
count = count + 1;
if (term < ACCURACY)
n= 999;
else
n = n + 1;
}
printf(“Terms = %d SUM = %f \n”, count, sum);
}
Output :
Enter value of x:0
Terms = 2 sum = 1.000000
Enter value of x:0.1
Terms = 5 sum = 1.105171
Enter value of x:0.5
Terms = 7 sum = 1.648720
Enter value of x:0.75
Terms = 8 sum = 2.116997
Enter value of x:0.99
Terms = 9 sum = 2.691232
Enter value of x:1
Terms = 9 sum = 2.718279
0 Comments