Program to find the sum of series 1+X+X^3...+X^N

Problem statement:- Program to find the sum of series 1+X+X^3...+X^N

 Data requirement:-

   Input Data:- n, x

  Output Data:- sum

  Additional Data:- i

Program in C
  
Here is the source code of the C Program to find the sum of series 1+X+X^3...+X^N.

Code:
  
#include<stdio.h>
#include<math.h>
int main()
{
    int n,sum=0,i=1,x;
    printf("Enter the range of number:");
    scanf("%d",&n);
    printf("Enter the value of x:");
    scanf("%d",&x);
    while(i<=n)
    {
        sum+=pow(x,i);
        i+=2;
    }
    printf("The sum of the series = %d",sum);
}

Input/Output:
Enter the range of number:5
Enter the value of x:2
The sum of the series = 42

Program in C++
  
Here is the source code of the C Program to find the sum of series 1+X+X^3...+X^N.

Code:
  
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,sum=0,i=1,x;
    cout<<"Enter the range of number:";
    cin>>n;
    cout<<"Enter the value of x:";
    cin>>x;
    while(i<=n)
    {
        sum+=pow(x,i);
        i+=2;
    }
    cout<<"The sum of the series = "<<sum;
}

Input/Output:
Enter the range of number:8
Enter the value of x:4
The sum of the series = 17476

Program in Java
  
Here is the source code of the Java Program to find the sum of series 1+X+X^3...+X^N.

Code:

import java.util.Scanner;

public class p6 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
    int n,sum=0,i=1,x;
    System.out.println("Enter the range of number:");
    n=cs.nextInt();
    System.out.println("Enter the value of x:");
    x=cs.nextInt();
    while(i<=n)
    {
        sum+=Math.pow(x,i);
        i+=2;
    }
    System.out.println("The sum of the series = "+sum);
    cs.close();
}
}

Input/Output:
Enter the range of number:
3
Enter the value of x:
5
The sum of the series = 130

Program in Python
  
Here is the source code of the Python Program to find the sum of series 1+X+X^3...+X^N.

Code:


print("Enter the range of number:")
n=int(input())
print("Enter the value of x:");
x=int(input())
sum=0
i=1
while(i<=n):
    sum+=pow(x,i)
    i+=2
print("The sum of the series = ",sum)

Input/Output:
Enter the range of number:
9
Enter the value of x:
2
The sum of the series =  682


Post a Comment

1 Comments

  1. please correct the code in place of sum we have to put the sum=1

    ReplyDelete

Please do not Enter any spam link in the comment box