Program to Find the Sum of Series 1+x+x^2+……+x^n

Posted in

Program to Find the Sum of Series 1+x+x^2+……+x^n
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Problem Statement

    We need to write a program that prints the sum of a special series 1+x+x^2+……+x^n . Here x is a variable and n represents the power up to which the series will go. For instance, consider that a user inputs x = 5 and n=6.

    Input

    x=5
    n=6

    Output

    19531

    The x and n values entered as 5 and 6, respectively, will follow this pattern: 1 + 5+ 5 2 + 5 3 +  5 4 + 5 5 + 5 6 = 19531

    Solution

    Here are the steps that you need to follow to create a solution for the problem stated above:

    • Initialize a variable sum with an initial value of 1.
    • Ask the user to enter the values of x and n.
    • Create a loop from range 1 to n.
    • Inside the loop, add the value of x to the power n to the variable sum.

    C Program to Find the Sum of Series 1+x+x^2+……+x^n

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {	
    	int sum =1, x, n, i;
    	printf("Here we will calculate the sum of series 1+ x + x^2+ ...+ x^n \n");
    	printf("Enter the value of x: ");
    	scanf("%d", &x);
    	
    	printf("Enter the value of n: ");
    	scanf("%d", &n);
    	
    	for(i=1; i<=n;i++)
    	{
    		sum+=pow(x,i);
    	}
    	
    	printf("The sum of the series is: %d", sum);
    
    	return 0;
    }

    Output:

    Here we will calculate the sum of series 1+ x + x^2+ ...+ x^n
    Enter the value of x: 5
    Enter the value of n: 7
    The sum of the series is: 97656

    C++ Program to Find the Sum of Series 1+x+x^2+……+x^n

    #include<include>
    #include<math.h>
    using namespace std;
    int main()
    {
    	int sum=1,x,n,i; 
    	cout<<"Here we will calculate the sum of series 1+ x + x^2+ ...+ x^n \n"; 
    	
    	//input the value of x and n	
    	cout<<"Enter the value of x: "; cin>>x; 
    	cout<<"Enter the value of n: "; cin>>n; 	
    	
    	for(i=1;i<=n;i++)
    	{
    		sum += pow(x,i);
    	}
    	cout<<"The sum of the series is: "<<sum;
    	return 0; 
    }

    Output:

    Here we will calculate the sum of series 1 +x +x^2 +……+ x^n
    
    Enter the value of x: 5
    Enter the value of n: 7
    The sum of the series is: 97656

    Python Program to Find the Sum of Series 1+x+x^2+……+x^n

    import math
    sum=1
    print("Here we will calculate the sum of series 1 +x +x^2 +……+ x^n")
    x=int(input("Enter the value of x: "))
    n = int(input("Enter the value of n: "))
    for i in range(1,n+1):
        sum+=math.pow(x,i)
    print("The sum of the series is:", int(sum))

    Output:

    Here we will calculate the sum of series 1 +x +x^2 +……+ x^n
    
    Enter the value of x: 3
    Enter the value of n: 4
    The sum of the series is: 121

    Java Program to Find the Sum of Series 1+x+x^2+……+x^n

    import java.util.Scanner;
    import java.lang.Math;
    public class Main
    {
    	public static void main(String[] args) {
            
        	int sum = 1, x, n, i; 
        	Scanner sc = new Scanner (System.in);
        	System.out.println("We will calculate the sum of series 1+ x + x^2+ ...+ x^n \n"); 
        	
        	//input the value of x and n
        	System.out.println("Enter the valyue of x:");
        	x = sc.nextInt(); 
        	System.out.println("Enter the valyue of n:");
        	n = sc.nextInt(); 
        	
        	for(i=1;i<=n;i++)
        	{
        		sum += Math.pow(x,i);
        	}
        	System.out.println("The sum of the series is: " +sum);
    	}
    }

    Output

    We will calculate the sum of series 1+ x + x^2+ ...+ x^n 
    
    Enter the valyue of x:
    3
    Enter the valyue of n:
    4
    The sum of the series is: 121

    Wrapping Up

    In this programming tutorial, we discussed how to find the sum of series 1+x+x^2+……+x^n. We have written programs in C, C++, Python, and Java. To calculate the sum, we used the for loop . The time complexity of the above program is O(N), and the space complexity is constant.

    If you have any queries, issues, or suggestions, please let us know by commenting them down below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments