📌  相关文章
📜  程序来查找级数之和(1 / a + 2 / a ^ 2 + 3 / a ^ 3 +…+ n / a ^ n)

📅  最后修改于: 2021-04-27 23:37:08             🧑  作者: Mango

给定两个整数a   n   。任务是找到1 / a + 2 / a 2 + 3 / a 3 +…+ n / a n的和
例子:

方法:运行从1到n的循环并获得i-th   通过计算项=(i / a i )来级数的。总结所有生成的术语,这是最终答案。
下面是上述方法的实现:

C++
// C++ program to find the sum of
// the given series
#include 
#include 
#include 
 
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


Javascript


输出:
0.6666667