📜  求出1 + 11 + 111 + 1111 +…..至n项的和。

📅  最后修改于: 2021-05-07 07:22:46             🧑  作者: Mango

在这里,我们将找到级数1 + 11 + 111 + 1111 +…..upto N个项的总和(其中N是给定的)。
例子 :

Input : 3
Output : 1 + 11 + 111 +....
Total sum is : 123

Input : 4
Output : 1 + 11 + 111 + 1111 +..... 
Total sum is : 1234

Input : 7
Output : 1 + 11 + 111 + 1111 + 11111 + 
         111111 + 1111111 +..... 
Total sum is : 1234567

在这里,我们看到,当N的值为3时,序列的最后值为1 + 11 + 111,即三个项,其和为123。
查找上述系列的总和的程序:

C++
// C++ program to find the sum of
// the series 1+11+111+1111+....
#include 
using namespace std;
 
// Function for finding summation
int summation(int n)
{
    int sum = 0, j = 1;
    for (int i = 1; i <= n; i++) {
        sum = sum + j;
 
        // Appending a 1 at the end
        j = (j * 10) + 1;
    }
 
    return sum;
}
 
// Driver Code
int main()
{
    int n = 5;
    cout << " " <<  summation(n);
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C program to find the sum of
// the series 1+11+111+1111+....
#include 
 
// Function for finding summation
int summation(int n)
{
    int sum = 0, j = 1;
    for (int i = 1; i <= n; i++) {
        sum = sum + j;
 
        // Appending a 1 at the end
        j = (j * 10) + 1;
    }
 
    return sum;
}
 
// Driver Code
int main()
{
    int n = 5;
    printf("%d", summation(n));
    return 0;
}


Java
// Java program to find the sum of
// the series 1+11+111+1111+....
import java.io.*;
 
class GFG
{
 
    // Function for finding summation
    static int summation(int n)
    {
        int sum = 0, j = 1;
        for (int i = 1; i <= n; i++)
        {
            sum = sum + j;
            j = (j * 10) + 1;
        }
 
        return sum;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(summation(n));
    }
}
 
// This code is contributed
// by Nikita Tiwari


Python
# Python program to get the summation
# of following series
def summation(n):
    sum = 0
    j = 1
     
    for i in range(1, n + 1):
        sum = sum + j
        j = (j * 10) + 1
         
    return sum
         
# Driver Code
n = 5
print(summation(n))


C#
// C# program to find the sum of
// the series 1+11+111+1111+....
using System;
 
class GFG
{
 
    // Function for finding summation
    static int summation(int n)
    {
        int sum = 0, j = 1;
        for (int i = 1; i <= n; i++)
        {
            sum = sum + j;
            j = (j * 10) + 1;
        }
 
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 5;
        Console.WriteLine(summation(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// C++ program to find the sum of
// the series 1+11+111+1111+....
#include 
 
// Function for finding summation
int summation(int n)
{
    int sum;
 
    sum = (pow(10, n + 1) -
               10 - (9 * n)) / 81;
 
    return sum;
}
 
// Driver Code
int main()
{
    int n = 5;
    printf("%d", summation(n));
    return 0;
}


Java
// java program to find the sum of
// the series 1+11+111+1111+....
import java.io.*;
 
class GFG {
 
    // Function for finding summation
    static int summation(int n)
    {
        int sum;
     
        sum = (int)(Math.pow(10, n + 1) -
                10 - (9 * n)) / 81;
     
        return sum;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        System.out.println(summation(n));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Python3 program to
# find the sum of
# the series 1+11+111+1111+....
import math
 
# Function for
# finding summation
def summation(n):
    return int((pow(10, n + 1) -
                    10 - (9 * n)) / 81);
 
# Driver Code
print(summation(5));
 
# This code is contributed
# by mits.


C#
// C# program to find the sum of
// the series 1+11+111+1111+....
using System;
 
class GFG {
 
    // Function for finding summation
    static int summation(int n)
    {
        int sum;
     
        sum = (int)(Math.Pow(10, n + 1) -
                10 - (9 * n)) / 81;
     
        return sum;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 5;
        Console.WriteLine(summation(n));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

12345

另一种方法:让给定级数S = 1 + 11 + 111 + 1111 +。 。 。 +至第n个学期。用公式求级数之和。

下面是上述方法的实现。

C++

// C++ program to find the sum of
// the series 1+11+111+1111+....
#include 
 
// Function for finding summation
int summation(int n)
{
    int sum;
 
    sum = (pow(10, n + 1) -
               10 - (9 * n)) / 81;
 
    return sum;
}
 
// Driver Code
int main()
{
    int n = 5;
    printf("%d", summation(n));
    return 0;
}

Java

// java program to find the sum of
// the series 1+11+111+1111+....
import java.io.*;
 
class GFG {
 
    // Function for finding summation
    static int summation(int n)
    {
        int sum;
     
        sum = (int)(Math.pow(10, n + 1) -
                10 - (9 * n)) / 81;
     
        return sum;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        System.out.println(summation(n));
    }
}
 
// This code is contributed by anuj_67.

Python3

# Python3 program to
# find the sum of
# the series 1+11+111+1111+....
import math
 
# Function for
# finding summation
def summation(n):
    return int((pow(10, n + 1) -
                    10 - (9 * n)) / 81);
 
# Driver Code
print(summation(5));
 
# This code is contributed
# by mits.

C#

// C# program to find the sum of
// the series 1+11+111+1111+....
using System;
 
class GFG {
 
    // Function for finding summation
    static int summation(int n)
    {
        int sum;
     
        sum = (int)(Math.Pow(10, n + 1) -
                10 - (9 * n)) / 81;
     
        return sum;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 5;
        Console.WriteLine(summation(n));
    }
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出 :

12345