Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
You have been given a series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n, find out the sum of the series till nth term.
Examples:
Input : n = 3 Output : 1.28704 Explanation : 1 + 1/2^2 + 1/3^3 Input : n = 5 Output : 1.29126 Explanation : 1 + 1/2^2 + 1/3^3 + 1/4^4 + 1/5^5
We use use power function to compute power.
C/C++
// C program to calculate the following series #include <math.h> #include <stdio.h> // Function to calculate the following series double Series(int n) { int i; double sums = 0.0, ser; for (i = 1; i <= n; ++i) { ser = 1 / pow(i, i); sums += ser; } return sums; } // Driver Code int main() { int n = 3; double res = Series(n); printf("%.5f", res); return 0; } |
chevron_right
filter_none
Java
// Java program to calculate the following series import java.io.*; class Maths { // Function to calculate the following series static double Series(int n) { int i; double sums = 0.0, ser; for (i = 1; i <= n; ++i) { ser = 1 / Math.pow(i, i); sums += ser; } return sums; } // Driver Code public static void main(String[] args) { int n = 3; double res = Series(n); res = Math.round(res * 100000.0) / 100000.0; System.out.println(res); } } |
chevron_right
filter_none
Python
# Python program to calculate the following series def Series(n): sums = 0.0 for i in range(1, n + 1): ser = 1 / (i**i) sums += ser return sums # Driver Code n = 3res = round(Series(n), 5) print(res) |
chevron_right
filter_none
C#
// C# program to calculate the following series using System; class Maths { // Function to calculate the following series static double Series(int n) { int i; double sums = 0.0, ser; for (i = 1; i <= n; ++i) { ser = 1 / Math.Pow(i, i); sums += ser; } return sums; } // Driver Code public static void Main() { int n = 3; double res = Series(n); res = Math.Round(res * 100000.0) / 100000.0; Console.Write(res); } } /*This code is contributed by vt_m.*/ |
chevron_right
filter_none
PHP
<?php // PHP program to calculate // the following series // Function to calculate // the following series function Series($n) { $i; $sums = 0.0; $ser; for ($i = 1; $i <= $n; ++$i) { $ser = 1 / pow($i, $i); $sums += $ser; } return $sums; } // Driver Code $n = 3; $res = Series($n); echo $res; // This code is contributed by Vishal Tripathi. ?> |
chevron_right
filter_none
output:
1.28704
Output:
1.28704
Recommended Posts:
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find Nth term of the series 3, 12, 29, 54, 87, ...
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find the Nth term of the series 0, 5, 18, 39, 67, 105, 150, 203, ...
- Program to find sum of harmonic series
- Program to find the Nth term of the series 0, 3/1, 8/3, 15/5........
- Program to find Nth term of series 0, 10, 30, 60, 99, 150, 210, 280...........
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m



