Related Articles
Sum of N terms in the expansion of Arcsin(x)
• Last Updated : 27 Aug, 2019

Given two integers N and X, the task is to find the value of Arcsin(x) using expansion upto N terms.

Examples:

Input: N = 4, X = 0.5
Output: 0.5233863467
Sum of first 4 terms in the expansion of Arcsin(x) for
x = 0.5 is 0.5233863467.

Input: N = 8, X = -0.5
Output: -0.5233948501

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The expansion of arcsin(x) is given by : Note: |x| < 1

The above expansion is solved by using two variables maintaining the numerator and the denominator.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the arcsin(x) ` `void` `find_Solution(``double` `x, ``int` `n) ` `{ ` `    ``double` `sum = x, e = 2, o = 1, p = 1; ` `    ``for` `(``int` `i = 2; i <= n; i++) { ` ` `  `        ``// The power to which 'x' is raised ` `        ``p += 2; ` ` `  `        ``sum += (``double``)(o / e) * (``double``)(``pow``(x, p) / p); ` ` `  `        ``// Numerator value ` `        ``o = o * (o + 2); ` ` `  `        ``// Denominator value ` `        ``e = e * (e + 2); ` `    ``} ` `    ``cout << setprecision(10) << sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``double` `x = -0.5; ` ` `  `    ``if` `(``abs``(x) >= 1) { ` `        ``cout << ``"Invalid Input\n"``; ` `        ``return` `0; ` `    ``} ` ` `  `    ``int` `n = 8; ` `    ``find_Solution(x, n); ` `    ``return` `0; ` `} `

## Java

 `//Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find the arcsin(x) ` `static` `void` `find_Solution(``double` `x, ``int` `n) ` `{ ` `    ``double` `sum = x, e = ``2``, o = ``1``, p = ``1``; ` `    ``for` `(``int` `i = ``2``; i <= n; i++)  ` `    ``{ ` ` `  `        ``// The power to which 'x' is raised ` `        ``p += ``2``; ` ` `  `        ``sum += (``double``)(o / e) *  ` `               ``(``double``)(Math.pow(x, p) / p); ` ` `  `        ``// Numerator value ` `        ``o = o * (o + ``2``); ` ` `  `        ``// Denominator value ` `        ``e = e * (e + ``2``); ` `    ``} ` `    ``System.out.println (sum); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``double` `x = -``0.5``; ` ` `  `    ``if` `(Math.abs(x) >= ``1``) ` `    ``{ ` `        ``System.out.println (``"Invalid Input"``); ` `    ``} ` `     `  `    ``int` `n = ``8``; ` `    ``find_Solution(x, n); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to find the arcsin(x) ` `def` `find_Solution(x, n): ` `    ``Sum` `=` `x ` `    ``e ``=` `2` `    ``o ``=` `1` `    ``p ``=` `1` `    ``for` `i ``in` `range``(``2``, n ``+` `1``): ` ` `  `        ``# The power to which 'x' is raised ` `        ``p ``+``=` `2` ` `  `        ``Sum` `+``=` `(o ``/` `e) ``*` `(``pow``(x, p) ``/` `p) ` ` `  `        ``# Numerator value ` `        ``o ``=` `o ``*` `(o ``+` `2``) ` ` `  `        ``# Denominator value ` `        ``e ``=` `e ``*` `(e ``+` `2``) ` `    ``print``(``round``(``Sum``, ``10``)) ` ` `  `# Driver code ` `x ``=` `-``0.5` ` `  `if` `(``abs``(x) >``=` `1``): ` `    ``print``(``"Invalid Input\n"``) ` ` `  `n ``=` `8` `find_Solution(x, n) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG  ` `{ ` ` `  `// Function to find the arcsin(x) ` `static` `void` `find_Solution(``double` `x, ``int` `n) ` `{ ` `    ``double` `sum = x, e = 2, o = 1, p = 1; ` `    ``for` `(``int` `i = 2; i <= n; i++)  ` `    ``{ ` ` `  `        ``// The power to which 'x' is raised ` `        ``p += 2; ` ` `  `        ``sum += (``double``)(o / e) *  ` `               ``(``double``)(Math.Pow(x, p) / p); ` ` `  `        ``// Numerator value ` `        ``o = o * (o + 2); ` ` `  `        ``// Denominator value ` `        ``e = e * (e + 2); ` `    ``} ` `    ``Console.WriteLine(sum); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main (String[] args)  ` `{ ` `    ``double` `x = -0.5; ` ` `  `    ``if` `(Math.Abs(x) >= 1) ` `    ``{ ` `        ``Console.WriteLine(``"Invalid Input"``); ` `    ``} ` `     `  `    ``int` `n = 8; ` `    ``find_Solution(x, n); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```-0.5233948501
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :