0% found this document useful (0 votes)
20 views

C Pgms & C++ With DS Algo With Explanation

The C program calculates the sine, cosine, and exponential series for a given value of x and number of terms n. It takes x and n as input, converts x to radians, then calculates the terms of the series in a for loop and sums them to find the approximate value of sinx, cosx, or ex. The program outputs the calculated value. It works by taking the user input, converting to radians, initializing the term and sum variables, then iterating through a for loop to calculate each term and add it to the running sum to approximate the function value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Pgms & C++ With DS Algo With Explanation

The C program calculates the sine, cosine, and exponential series for a given value of x and number of terms n. It takes x and n as input, converts x to radians, then calculates the terms of the series in a for loop and sums them to find the approximate value of sinx, cosx, or ex. The program outputs the calculated value. It works by taking the user input, converting to radians, initializing the term and sum variables, then iterating through a for loop to calculate each term and add it to the running sum to approximate the function value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ex:1 C program for Sine Series

Sine Series:
                Sine Series is a series which is used to find the value of Sin(x).
Where, x is the angle in degree which is converted to Radian.
Formula to express the Sin(x) as Sine Series is:

Expanding the above notation, the formula of Sine Series is

For example,
                Let the value of x be 30.

So, Radian value for 30 degree is 0.52359.

So, the value of Sin(30) is 0.5.


Program code for Sine Series in C:
1. #include<stdio.h>
2. #include<conio.h>

3. void main()
4. {
5. int i, n;
6. float x, sum, t;
7. clrscr();
8. printf(" Enter the value for x : ");
9. scanf("%f",&x);
10. printf(" Enter the value for n : ");
11. scanf("%d",&n);
12. x=x*3.14159/180;
13. t=x;
14. sum=x;
15. /* Loop to calculate the value of Sine */
16. for(i=1;i<=n;i++)
17. {
18. t=(t*(-1)*x*x)/(2*i*(2*i+1));
19. sum=sum+t;
20. }

21. printf(" The value of Sin(%f) = %.4f",x,sum);


22. getch();
23. }

Output:

Working:
Algorithm:
 First the computer reads the value of  ‘x’ and ‘n’ from the user.
 Then ‘x’ is converted to radian value.
 Then using for loop the value of Sin(x) is calculate.
 Finally the value of Sin(x) is printed.
Step by Step working of the above Program Code:
Let us assume that the user enters the value of ‘x’ as 45 and ‘n’ as 4.
1. Converting ‘x’ to radian value
x = x * 3.14159 / 180    (x = 45 * 3.14159 / 180)    So,  x=0.785398
2. It assigns t=x and sum=x (i.e. t=0.785398 and sum=0.785398)
2. It assigns the value of i=1 and the loop continues till the condition of the for
loop is true.
3.1.    i<=n    (1<=4)    for loop condition is TRUE
t = (0.785398 * (-1) * 0.785398 * 0.785398)/(2 * 1 * (2 * 1 + 1))
So,  t = – 0.08074
sum = 0.785398 + (- 0.08074)
So,  sum=0.70465
i++
So,  i=2
3.2.    i<=n    (2<=4)    for loop condition is TRUE
t = (- 0.08074 * (-1) * 0.785398 * 0.785398)/(2 * 2 * (2 * 2 + 1))
So,  t = 0.00249
sum = 0.70465 + 0.00249
So,  sum=0.70714
i++
So,  i=3
3.3.    i<=n    (3<=4)    for loop condition is TRUE
t = (0.00249 * (-1) * 0.785398 * 0.785398)/(2 * 3 * (2 * 3 + 1))
So,  t = – 0.000032
sum = 0.70714 + (- 0.000032)
So,  sum=0.707108
i++
So,  i=4
3.4.   i<=n    (4<=4)    for loop condition is TRUE
t = (- 0.000032 * (-1) * 0.785398 * 0.785398)/(2 * 4 * (2 * 4 + 1))
So,  t = 0.000000274
sum = 0.707108 + 0.000000274
So,  sum=0.707108274
i++
So,  i=5
3.5.  i<=n    (5<=4)    for loop condition is FALSE
It comes out of the for loop.
4. Finally it prints The value of  Sin(0.785398) = 0.7071
5. Thus program execution is completed.
Let us assume that the user enters the value of ‘x’ as 45 and ‘n’ as 4.
1. Converting ‘x’ to radian value
x = x * 3.14159 / 180    (x = 45 * 3.14159 / 180)    So,  x=0.785398
2. It assigns t=x and sum=x (i.e. t=0.785398 and sum=0.785398)
2. It assigns the value of i=1 and the loop continues till the condition of the for
loop is true.
3.1.    i<=n    (1<=4)    for loop condition is true
t = (0.785398 * (-1) * 0.785398 * 0.785398)/(2 * 1 * (2 * 1 + 1))
So,  t = – 0.08074
sum = 0.785398 + (- 0.08074)
So,  sum=0.70465
i++
So,  i=2
3.2.    i<=n    (2<=4)    for loop condition is true
t = (- 0.08074 * (-1) * 0.785398 * 0.785398)/(2 * 2 * (2 * 2 + 1))
So,  t = 0.00249
sum = 0.70465 + 0.00249
So,  sum=0.70714
i++
So,  i=3
3.3.   i<=n    (3<=4)    for loop condition is true
t = (0.00249 * (-1) * 0.785398 * 0.785398)/(2 * 3 * (2 * 3 + 1))
So,  t = – 0.000032
sum = 0.70714 + (- 0.000032)
So,  sum=0.707108
i++
So,  i=4
3.4.    i<=n    (4<=4)    for loop condition is true
t = (- 0.000032 * (-1) * 0.785398 * 0.785398)/(2 * 4 * (2 * 4 + 1))
So,  t = 0.000000274
sum = 0.707108 + 0.000000274
So,  sum=0.707108274
i++
So,  i=5
3.5. i<=n    (5<=4)    for loop condition is false
It comes out of the for loop.
4. Finally it prints The value of  Sin(0.785398) = 0.7071
5. Thus program execution is completed.
C program for Cosine Series

Cosine Series:
                Cosine Series is a series which is used to find the value of Cos(x).
where, x is the angle in degree which is converted to Radian.

Formula used to express the Cos(x) as Cosine Series is

Expanding the above notation, the formula of Cosine Series is

For example,
                Let the value of x be 30.

So, Radian value for 30 degree is 0.52359.

So, the value of Cos(30) is 0.8660.


Program code for Cosine Series in C:

1. #include<stdio.h>
2. #include<conio.h>

3. void main()
4. {
5. int i, n;
6. float x, sum=1, t=1;
7. clrscr();
8. printf(" Enter the value for x : ");
9. scanf("%f",&x);
10. printf(" Enter the value for n : ");
11. scanf("%d",&n);
12. x=x*3.14159/180; 
13. /* Loop to calculate the value of Cosine */
14. for(i=1;i<=n;i++)
15. {
16. t=t*(-1)*x*x/(2*i*(2*i-1));
17. sum=sum+t;
18. }
19. printf(" The value of Cos(%f) is : %.4f", x, sum);
20. getch();
21. }
Output:

Working:
Algorithm:
 First the computer reads the value of  ‘x’ and ‘n’ from the user.
 Then ‘x’ is converted to radian value.
 Then using for loop the value of Cos(x) is calculate.
 Finally the value of Cos(x) is printed.
Step by Step working of the above Program Code:
Let us assume that the user enters the value of ‘x’ as 30 and ‘n’ as 4.
1. Converting ‘x’ to radian value
x = x * 3.14159 / 180    (x = 30 * 3.14159 / 180)    So,  x=0.52359
2. It assigns t=1 and sum=1
3. It assigns the value of i=1 and the loop continues till the condition of the for
loop is true.
3.1.    i<=n    (1<=4)    for loop condition is true
t = (1 * (-1) * 0.52359 * 0.52359)/(2 * 1 * (2 * 1 – 1))
So,  t = – 0.13707
sum = 1 + (- 0.1370)
So,  sum=0.86293
i++
So,  i=2
3.2.   i<=n    (2<=4)    for loop condition is true
t = (- 0.13707 * (-1) * 0.52359 * 0.52359)/(2 * 2 * (2 * 2 – 1))
So,  t = 0.00313
sum = 0.86293 + 0.00313
So,  sum=0.86606
i++
So,  i=3
3.3.    i<=n    (3<=4)    for loop condition is true
t = (0.00313 * (-1) * 0.52359 * 0.52359)/(2 * 3 * (2 * 3 – 1))
So,  t = – 0.000028
sum = 0.86606 + (- 0.000028)
So,  sum=0.86603
i++
So,  i=4
3.4.    i<=n    (4<=4)    for loop condition is true
t = (-0.000028 * (-1) * 0.52359 * 0.52359)/(2 * 4 * (2 * 4 – 1))
So,  t = 0.000000137
sum = 0.86603 + 0.000000137
So,  sum=0.866030137
i++
So,  i=5
3.5.   i<=n    (5<=4)    for loop condition is false
It comes out of the for loop.
4. Finally it prints The value of  Cos(0.52359) is : 0.8660
5. Thus program execution is completed.
Ex:3 C program for Exponential Series

Exponential Series:
                Exponential Series is a series which is used to find the value of ex.
Formula used to express the ex as Exponential Series is

Expanding the above notation, the formula of Exponential Series is

For example,
                Let the value of x be 3.

So, the value of e3 is 20.0855


/* Program for Exponential Series */
#include<stdio.h>
#include<conio.h>
 
void main()
{
    int i, n;
    float x, sum=1, t=1;
    clrscr();
     
    printf("Enter the value for x : ");
    scanf("%f", &x);
     
    printf("\nEnter the value for n : ");
    scanf("%d", &n);
     
    /* Loop to calculate the value of Exponential */
    for(i=1;i<=n;i++)
    {
        t=t*x/i;
        sum=sum+t;
    }
     
    printf("\nThe Exponential Value of %f = %.4f", x, sum);
    getch();
}
Output:

Working:
Algorithm:
 First the computer reads the value of  ‘x’ and ‘n’ from the user.
 Then using for loop the value of ex is calculate.
 Finally the value of ex is printed.

Step by Step working of the above Program Code:


Let us assume that the user enters the value of ‘x’ as 3 and ‘n’ as 10.
1. It assigns t=1 and sum=1.
2. It assigns the value of i=1 and the loop continues till the condition of the
for loop is true.
2.1.    i<=n    (1<=10)    for loop condition is true
t = 1 * 3 / 1
So,  t = 3
sum = 1 + 3
So,  sum = 4
i++
So,  i=2
2.2.    i<=n    (2<=10)    for loop condition is true
t=3*3/2
So,  t = 4.5
sum = 4 + 4.5
So,  sum = 8.5
i++
So,  i=3
2.3.    i<=n    (3<=10)    for loop condition is true
t = 4.5 * 3 / 3
So,  t = 4.5
sum = 8.5 + 4.5
So,  sum=13
i++
So,  i=4
2.4.    i<=n    (4<=10)    for loop condition is true
t = 4.5 * 3 / 4
So,  t = 3.375
sum = 13 + 3.375
So,  sum=16.375
i++
So,  i=5
2.5.    i<=n    (5<=10)    for loop condition is true
t = 3.375 * 3 / 5
So,  t = 2.025
sum = 16.375 + 2.025
So,  sum=18.4
i++
So,  i=6
2.6.    i<=n    (6<=10)    for loop condition is true
t = 2.025 * 3 / 6
So,  t = 1.0125
sum = 18.4 + 1.0125
So,  sum=19.4125
i++
So,  i=7
2.7.    i<=n    (7<=10)    for loop condition is true
t = 1.0125 * 3 / 7
So,  t = 0.4339
sum = 19.4125 + 0.4339
So,  sum=19.8464
i++
So,  i=8
2.8.    i<=n    (8<=10)    for loop condition is true
t = 0.4339 * 3 / 8
So,  t = 0.1627
sum = 19.8464 + 0.1627
So,  sum=20.0091
i++
So,  i=9
2.9.    i<=n    (9<=10)    for loop condition is true
t = 0.1627 * 3 / 9
So,  t = 0.0542
sum = 20.0091 + 0.0542
So,  sum=20.0634
i++
So,  i=10
2.10.   i<=n    (10<=10)    for loop condition is true
t = 0.0524 * 3 / 10
So,  t = 0.01627
sum = 20.0634 + 0.01627
So,  sum=20.07966
i++
So,  i=11
2.11.  i<=n    (11<=10)    for loop condition is false
It comes out of the for loop.
3. Finally it prints The Exponential value of  3.000000 = 20.07966
4. Thus program execution is completed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy