Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n Last Updated : 04 Aug, 2022 Comments Improve Suggest changes 2 Likes Like Report 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^3Input : n = 5 Output : 1.29126 Explanation : 1 + 1/2^2 + 1/3^3 + 1/4^4 + 1/5^5 We use power function to compute power. C++ // C++ program to calculate the following series #include <bits/stdc++.h> using namespace std; // 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); cout << res; return 0; } // This code is contributed by Ankita saini 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; } 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); } } Python3 # 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 = 3 res = round(Series(n), 5) print(res) 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.*/ 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. ?> JavaScript <script> // Javascript program to calculate // the following series function Series(n) { let sums = 0.0; for(let i = 1; i < n + 1; i++) { ser = 1 / Math.pow(i, i); sums += ser; } return sums; } // Driver Code let n = 3; let res = Math.round(Series(n) * 100000) / 100000; document.write(res); // This code is contributed by rohitsingh07052 </script> Output: 1.28704 Time Complexity: O(nlogn) since using inbuilt pow function in loop Auxiliary Space: O(1) Create Quiz Comment C chinmoy lenka Follow 2 Improve C chinmoy lenka Follow 2 Improve Article Tags : DSA series Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like