Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Question 5: Write a C++ program with function ‘easySin()’ that takes a number ‘x’ as
- input and calculates its simplified sine expansion series. The series expansion is
- given below, and you must calculate first 10 values of the series using loop.
- Sine Series:
- Sin(x) = x – x/3 + x/5 – x/7 + x/9 – x/11 + x/13 – x/15 + x/17 – x/19
- Expected output:
- Input: x = 5
- Output: Sin(5) = +5–5/3+5/5–5/7+5/9–5/11+5/13–5/15+5/17–5/19 = 3.8023
- Print the whole series as well the result on screen as shown below.
- */
- #include<iostream>
- using namespace std;
- void myIntro();
- int input_number(void);
- bool is_even(int);
- float easySine(int);
- int main()
- {
- int x = 0;
- float sineExp = 0;
- cout << "To compute y=Sin(x), enter a value for x: ";
- x = input_number();
- sineExp = easySine(x);
- cout << "\nSine(5)=" << sineExp;
- //print_EasySine(x);
- return 0;
- }
- void myIntro(void)
- {
- cout << "\nName: Muhammad Talha Dar";
- cout << "\nRegistration#: L1S19BSCS0060";
- cout << "\nSection: B";
- cout << "\n============================" << endl;
- }
- int input_number(void)
- {
- int a = 0;
- cout << "\nEnter Number: ";
- cin >> a;
- return a;
- }
- bool is_even(int a)
- {
- if (a % 2 == 0)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- float easySine(int x)
- {
- int i = 1, z = 1;
- float sin = 0;
- while (i <= 10 && z <= 19)
- {
- if (is_even(i) == 1)
- {
- sin = (sin - (x / z)*1.0);
- }
- else
- {
- sin = (sin + (x / z)*1.0);
- }
- z = z + 2;
- i = i + 1;
- }
- return sin;
- }
Add Comment
Please, Sign In to add comment