daaaar

easySine

Apr 27th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. /*Question 5: Write a C++ program with function ‘easySin()’ that takes a number ‘x’ as
  2. input and calculates its simplified sine expansion series. The series expansion is
  3. given below, and you must calculate first 10 values of the series using loop.
  4. Sine Series:
  5. Sin(x) = x – x/3 + x/5 – x/7 + x/9 – x/11 + x/13 – x/15 + x/17 – x/19
  6. Expected output:
  7. Input: x = 5
  8. Output: Sin(5) = +5–5/3+5/5–5/7+5/9–5/11+5/13–5/15+5/17–5/19 = 3.8023
  9. Print the whole series as well the result on screen as shown below.
  10. */
  11.  
  12. #include<iostream>
  13. using namespace std;
  14.  
  15. void myIntro();
  16. int input_number(void);
  17. bool is_even(int);
  18.  
  19. float easySine(int);
  20.  
  21. int main()
  22. {
  23.     int x = 0;
  24.     float sineExp = 0;
  25.     cout << "To compute y=Sin(x), enter a value for x: ";
  26.     x = input_number();
  27.     sineExp = easySine(x);
  28.     cout << "\nSine(5)=" << sineExp;
  29.     //print_EasySine(x);
  30.     return 0;
  31. }
  32.  
  33. void myIntro(void)
  34. {
  35.     cout << "\nName: Muhammad Talha Dar";
  36.     cout << "\nRegistration#: L1S19BSCS0060";
  37.     cout << "\nSection: B";
  38.     cout << "\n============================" << endl;
  39. }
  40.  
  41. int input_number(void)
  42. {
  43.     int a = 0;
  44.     cout << "\nEnter Number: ";
  45.     cin >> a;
  46.     return a;
  47. }
  48.  
  49. bool is_even(int a)
  50. {
  51.     if (a % 2 == 0)
  52.     {
  53.         return 1;
  54.     }
  55.     else
  56.     {
  57.         return 0;
  58.     }
  59. }
  60.  
  61. float easySine(int x)
  62. {
  63.     int i = 1, z = 1;
  64.     float sin = 0;
  65.     while (i <= 10 && z <= 19)
  66.     {
  67.         if (is_even(i) == 1)
  68.         {
  69.             sin = (sin - (x / z)*1.0);
  70.         }
  71.         else
  72.         {
  73.             sin = (sin + (x / z)*1.0);
  74.         }
  75.         z = z + 2;
  76.         i = i + 1;
  77.     }
  78.     return sin;
  79. }
Add Comment
Please, Sign In to add comment