Given two integers
and
. The task is to find the sum of the series 1/a + 2/a2 + 3/a3 + … + n/an.
Examples:
Input: a = 3, n = 3
Output: 0.6666667
The series is 1/3 + 1/9 + 1/27 which is
equal to 0.6666667Input: a = 5, n = 4
Output: 0.31039998
Approach: Run a loop from 1 to n and get the
term of the series by calculating term = (i / ai). Sum all the generated terms which is the final answer.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of // the given series #include <stdio.h> #include <math.h> #include <iostream> using namespace std; // Function to return the // sum of the series float getSum(int a, int n) { // variable to store the answer float sum = 0; for (int i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / pow(a, i)); } return sum; } // Driver code int main() { int a = 3, n = 3; // Print the sum of the series cout << (getSum(a, n)); return 0; } // This code is contributed // by Sach_Code |
Java
// Java program to find the sum of the given series import java.util.Scanner; public class HelloWorld { // Function to return the sum of the series public static float getSum(int a, int n) { // variable to store the answer float sum = 0; for (int i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / Math.pow(a, i)); } return sum; } // Driver code public static void main(String[] args) { int a = 3, n = 3; // Print the sum of the series System.out.println(getSum(a, n)); } } |
Python 3
# Python 3 program to find the sum of # the given series import math # Function to return the # sum of the series def getSum(a, n): # variable to store the answer sum = 0; for i in range (1, n + 1): # Math.pow(x, y) returns x^y sum += (i / math.pow(a, i)); return sum; # Driver code a = 3; n = 3; # Print the sum of the series print(getSum(a, n)); # This code is contributed # by Akanksha Rai |
C#
// C# program to find the sum // of the given series using System; class GFG { // Function to return the sum // of the series public static double getSum(int a, int n) { // variable to store the answer double sum = 0; for (int i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / Math.Pow(a, i)); } return sum; } // Driver code static public void Main () { int a = 3, n = 3; // Print the sum of the series Console.WriteLine(getSum(a, n)); } } // This code is contributed by jit_t |
PHP
<?php // PHP program to find the // sum of the given series // Function to return the // sum of the series function getSum($a, $n) { // variable to store the answer $sum = 0; for ($i = 1; $i <= $n; ++$i) { // Math.pow(x, y) returns x^y $sum += ($i / pow($a, $i)); } return $sum; } // Driver code $a = 3; $n = 3; // Print the sum of the series echo (getSum($a, $n)); // This code is contributed by akt_mit ?> |
0.6666667
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.
Recommended Posts:
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ 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 the series 1*3 + 3*5 + ....
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- 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 series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find the sum of the series 1 + x + x^2+ x^3+ .. + x^n
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2
- Program to find Nth term of series 3, 12, 29, 54, 86, 128, 177, 234, .....
- Program to find N-th term of series 3, 5, 33, 35, 53....
- Program to find Nth term of series 0, 9, 22, 39, 60, 85, 114, 147, .....
- Program to find Nth term of series 1, 6, 17, 34, 56, 86, 121, 162, .......
- Program to find Nth term of the series 3, 12, 29, 54, 87, ...
- Program to find sum of harmonic series
- Program to find Nth term of the series 3 , 5 , 21 , 51 , 95 , ...
- Program to find the nth term of the series -2, 4, -6, 8....
- Program to find nth term of the series 1 4 15 24 45 60 92
- Program to find the Nth term of the series 0, 3/1, 8/3, 15/5........
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.

