Object

Write a program to print sum of the series 1 + (1/x) + (2/x2) + (3/x3) + … + (n/xn).

Algorithm

  1. Declare integer variables i, x and n and float variable y.
  2. Set y=1.
  3. Read x and n.
  4. Repeat FOR i = 1 to n by 1
    • y = y + (i/xi)
    • [End of FOR loop.]
  5. Write y.
  6. Exit.

Flowchart

C++ Source Code

// program 36
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
  int i, x, n;
  float y = 1;
  cout << "The series is:\n";
  cout << "1 + 1/x + 2/x^2 + 3/x^3 + ... + n/x^n\n\n";
  cout << "Enter the value of x: ";
  cin >> x;
  cout << "Enter the value of n: ";
  cin >> n;

  for (i = 1; i <= n; i++)
    y = y + (i / pow(x, i));

  cout << "The sum of series = " << fixed << setprecision(3) << y;
  return 0;
}

C Source Code

/*program 36*/
#include<stdio.h>
#include<math.h>

int main()
{
  int i, x, n;
  float y=1;
  printf("The series is:\n");
  printf("1 + 1/x + 2/x^2 + 3/x^3 + ... + n/x^n");
  printf("\n\nEnter the value of x: ");
  scanf("%d", &x);
  printf("Enter the value of n: ");
  scanf("%d", &n);

  for(i=1; i<=n; i++)
     y=y+(i/(pow(x,i)));
  printf("The sum of series = %.3f", y);
  return 0;
}

Output

The series is:
1 + 1/x + 2/x^2 + 3/x^3 + … + n/x^n

Enter the value of x: 4
Enter the value of n: 5
The sum of series = 1.442

Design a site like this with WordPress.com
Get started