English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C program for the Nth term of a geometric series

Given the first term 'a', the common ratio 'r', and the number of terms in the series 'n'. The task is to find the nth term of the series.

Therefore, before discussing how to write a program for this problem, we should know what a geometric series is.

In mathematics, a geometric series or geometric sequence is found by multiplying the previous term by a fixed proportional term to find each term after the first term.

like2,4,8,16,32 .. is the first term of2and the common ratio is2geometric series. If n = 4Then the output is16.

Therefore, we can say that the geometric series of the nth term will be similar to-

GP1 = a1
GP2 = a1 * r^(2-1)
GP3 = a1 * r^(3-1)
. . .
GPn = a1 * r^(n-1)

Therefore the formula will be GP = a * r^(n-1)。

Example

Input: A=1
   R=2
   N=5
Output: The 5The nth term of the series is: 16
Explanation: The terms will be
   1, 2, 4, 8, 16 so the output will be 16
Input: A=1
   R=2
   N=8
Output: The 8th Term of the series is: 128

The method we will use to solve the given problem-

  • with the first term A, common ratio R, and N as the series number.

  • Then through A *(int)(pow(R,N-1)Calculate the nth term.

  • Returns the output obtained from the above calculation.

Algorithm

Start
   Step 1 -> In function int Nth_of_GP(int a, int r, int n)
      Return( * (int)(pow(r, - 1))
   Step 2 -> In function int main() Declare and set a = 1
      Declare and set r = 2
      Declare and set n = 8
      Print the output returned from calling the function Nth_of_GP(a, r, n)
Stop

Example

#include <stdio.h>
#include <math.h>
//The function returns the nth term of the GP
int Nth_of_GP(int a, int r, int n) {
   //the nth word is
   return( * (int)(pow(r, - 1))
}
//main block
int main() {
   //initial term
   int a = 1;
   //common ratio
   int r = 2;
   //the nth term
   int n = 8;
   printf("The %dth term of the series is: %d\n", n, Nth_of_GP(a, r, n));
   return 0;
}

Output Result

The 8The nth term of the series is: 128