Jump to content

Find terms of a geometric progression - C++

0
  Ángel Manuel García Carmona's Photo
Posted Feb 26 2011 12:57 PM

A geometric progression is a sequence of numbers obtained after multiplying the former by a constant called the rate of progression. For example: 5, 15, 45, etc., is a series whose ratio is 3.
Then to develop a program which process and show the terms of a progression, written in C + +, use the following code:


#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
 int numberterms;
 float firstterm, ratio;
  cout << "Insert the first term of the progression: ";
  cin >> firstterm;
  cout << "Insert the reason of this: ";
  cin >> ratio;
  cout << "Insert the number of terms to display: ";
  cin >> numberterms; 
  
  for (int r = 1; r <= numberterms; r++)
  {
   cout << firstterm;
   firstterm = firstterm*ratio;

   if (r != numberterms)
    cout << " ";
   if (r == numberterms)
    cout << endl << endl;
  }
}

Respect to the indicated in the code, first we ask what is the first term of the sequence, then the ratio (it can be also decimal or fractional) and finally, the number of terms of the succession to be displayed.

Tags:
0 Subscribe


1 Reply

 : Mar 07 2011 08:05 AM
Insert the reason of this?