Open In App

Program for sum of cosh(x) series upto Nth term

Last Updated : 25 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers x and N, the task is to find the value of cosh(x) from the series upto N terms.
The expansion of cosh(x) is given below: 
 

cosh(x) = 1 + x2/2! + x4/4! + …………

Examples: 
 

Input: x = 1, N = 5
Output: 1.54308035714

Input: x = 1, N = 10
Output: 1.54308063497

 

Approach: 
The above series can be easily implemented using a factorial function and loops.
The nth term of the series is: 
 

Below is the implementation of the above approach: 
 

C++




// C++ program for
// the sum of cosh(x) series
 
#include <bits/stdc++.h>
using namespace std;
 
// function to return the factorial of a number
int fact(int n)
{
 
    int i = 1, fac = 1;
    for (i = 1; i <= n; i++)
        fac = fac * i;
 
    return fac;
}
 
// function to return the sum of the series
double log_Expansion(double x, int n)
{
 
    double sum = 0;
    int i = 0;
 
    for (i = 0; i < n; i++) {
 
        sum = sum
              + pow(x, 2 * i)
                    / fact(2 * i);
    }
 
    return sum;
}
 
// Driver code
int main()
{
    double x = 1;
    int n = 10;
    cout << setprecision(12)
         << log_Expansion(x, n)
         << endl;
 
    return 0;
}


Java




// Java program for the sum of
// cosh(x) series
import java.util.*;
 
class GFG
{
 
// function to return the factorial of a number
static int fact(int n)
{
    int i = 1, fac = 1;
    for (i = 1; i <= n; i++)
        fac = fac * i;
 
    return fac;
}
 
// function to return the sum of the series
static double log_Expansion(double x, int n)
{
    double sum = 0;
    int i = 0;
 
    for (i = 0; i < n; i++)
    {
        sum = sum + Math.pow(x, 2 * i) /
                           fact(2 * i);
    }
 
    return sum;
}
 
// Driver code
public static void main(String[] args)
{
    double x = 1;
    int n = 10;
    System.out.println(log_Expansion(x, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program for the Sum of cosh(x) series
 
# function to return the factorial of a number
def fact(n):
 
    i, fac = 1, 1
    for i in range(1, n + 1):
        fac = fac * i
 
    return fac
 
# function to return the Sum of the series
def log_Expansion(x, n):
 
    Sum = 0
    i = 0
 
    for i in range(n):
 
        Sum = Sum + pow(x, 2 * i) / fact(2 * i)
 
    return Sum
 
# Driver code
x = 1
n = 10
print(log_Expansion(x, n))
 
# This code is contributed by Mohit Kumar


C#




// C# program for the sum of
// cosh(x) series
using System;
 
class GFG
{
 
// function to return the
// factorial of a number
static int fact(int n)
{
    int i = 1, fac = 1;
    for (i = 1; i <= n; i++)
        fac = fac * i;
 
    return fac;
}
 
// function to return the sum of the series
static double log_Expansion(double x, int n)
{
    double sum = 0;
    int i = 0;
 
    for (i = 0; i < n; i++)
    {
        sum = sum + Math.Pow(x, 2 * i) /
                        fact(2 * i);
    }
 
    return sum;
}
 
// Driver code
public static void Main(String[] args)
{
    double x = 1;
    int n = 10;
    Console.WriteLine(log_Expansion(x, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// Javascript program for the sum of
// cosh(x) series
 
    // function to return the factorial of a number
    function fact( n) {
        let i = 1, fac = 1;
        for (i = 1; i <= n; i++)
            fac = fac * i;
 
        return fac;
    }
 
    // function to return the sum of the series
    function log_Expansion( x , n) {
        let sum = 0;
        let i = 0;
 
        for (i = 0; i < n; i++) {
            sum = sum + Math.pow(x, 2 * i) / fact(2*i);
        }
 
        return sum;
    }
 
    // Driver code
      
        let x = 1;
        let n = 10;
        document.write(log_Expansion(x, n).toFixed(11));
 
// This code is contributed by shikhasingrajput
 
</script>


Output: 

1.54308063497

 

Time Complexity: O(n2), where n represents the value of the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Similar Reads

Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
Given two integers N and D, the task is to find the value of F(N) where the value of F(1) is D, where F(K) is given as: [Tex]F(K+1) = F(K) * (Max_{Digit}(F(K)) - Min_{Digit}(F(K)))[/Tex] Examples: Input: N = 3, D = 487 Output: 15584 Explanation: As F(1) = 487, F(2) = 487 * (maxDigit(487) - minDigit(487)) = 487 * 4 = 1948 F(3) = 1948 * (maxDigit(194
6 min read
Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series. Examples: Input : x = 9, n = 10 Output : -5.1463 Input : x = 5, n = 15 Output : 0.2837 Simple approach : We use two nested loops to compu
10 min read
Find Nth term of the series where each term differs by 6 and 2 alternately
Given a number N, the task is to find the Nth term of the series where each term differs by 6 and 2 alternately.Examples: Input: N = 6 Output: 24 Explanation: The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24Input: N = 3 Output: 14 Explanation: The Nth term is 0 + 6 + 2 + 6 = 14 Naive Approach: The idea is to iterate from 1 with an increment of 6 and
7 min read
Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
Given three integers X, Y and N, the task is to find the Nth term of the series f[i] = f[i - 1] - f[i - 2], i &gt; 1 where f[0] = X and f[1] = Y.Examples: Input: X = 2, Y = 3, N = 3 Output: -2 The series will be 2 3 1 -2 -3 -1 2 and f[3] = -2Input: X = 3, Y = 7, N = 8 Output: 4 Approach: An important observation here is that there will be atmost 6
10 min read
Program to print tetrahedral numbers upto Nth term
Prerequisites: Triangular numbersTetrahedral numbers Given a value n, and the task is to print tetrahedral number series up to nth term.Examples: Input: 5 Output: 1 4 10 20 35 Input: 10 Output: 1 4 10 20 35 56 84 120 165 220 Method 1: Using Triangular Number series: This problem can be easily solved with the fact that Nth Tetrahedral Number is equa
9 min read
Program to print pentatope numbers upto Nth term
Prerequisites: Tetrahedral numbersPentatope Numbers Given a value n, and the task is to print pentatope numbers series up to nth term.Examples: Input: 5 Output: 1 5 15 35 70 Input: 10 Output: 1 5 15 35 70 126 210 330 495 715 Method 1: Using Tetrahedral Number Series: This problem can be easily solved with the fact that Nth Pentatope Number is equal
10 min read
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
Given two numbers N and K, where K represents the starting term of the sequence. The task is to find the Nth term of a sequence formed by sum of current term with product of largest and smallest digit of current term, i.e., AN+1 = AN + max(digits of AN) * min(digits of AN) Examples: Input: K = 1, N = 5 Output: 50 Explanation: A1 = 1 A2 = A1 + minDi
6 min read
Sum of series till N-th term whose i-th term is i^k - (i-1)^k
Given value of N and K. The task is to find the sum of series till N-th term whose i-th term is given by Ti = ik + (i - 1)k. Since the sum of the series can be very large, compute its sum modulo 1000000007. Example: Input : n = 5, k = 2 Output: 25 first 5 term of the series : T1 = ( 1 )2 + ( 1 - 1 )2 = 1 T2 = ( 2 )2 + ( 2 - 1 )2 = 3 T3 = ( 3 )2 + (
9 min read
Nth term of given recurrence relation having each term equal to the product of previous K terms
Given two positive integers N and K and an array F[] consisting of K positive integers. The Nth term of the recurrence relation is given by: FN = FN - 1 * FN - 2 * FN - 3 *.......* FN - K The task is to find the Nth term of the given recurrence relation. As the Nth term can be very large, print the Nth term modulo 109 + 7. Examples: Input: N = 5, K
16 min read
First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
Given an integer N and an integer FN which denotes the Nth term of the linear equation F(N) = (2 * F(N - 1)) % M, where M is 109 + 7, the task is to find the value of F(1). Examples: Input : N = 2, FN = 6 Output:3 Explanation: If F(1) = 3, F(2) = (2 * F(1)) % M = (2 * 3) % M = 6. For F(1) = 3 the given linear equation satisfies the value of F(2). T
8 min read
Article Tags :
Practice Tags :