Skip to main content

Print the given geometric progression

A geometric progression (also known as a geometric sequence) is a sequence of numbers in which each term is obtained by multiplying the previous term by a fixed, non-zero number called the common ratio. Displaying a geometric progression is essential for understanding the growth pattern of numbers in such sequences. This article aims to explain how to print a given geometric progression using a C program.

Problem Statement

Given the first term 'a', common ratio 'r', and the number of terms 'n', the problem is to display the geometric progression of the given data.

Example

For instance, consider a geometric progression with a first term of 3, a common ratio of 2, and 6 terms. The geometric progression would be 3, 6, 12, 24, 48, and 96. Similarly, for a series with a first term of 2, a common ratio of 3, and 10 terms, the progression would be 2, 6, 18, 54, 162, 486, 1458, 4374, 13122, and 39366.

Idea to Solve

To solve this problem, you need to iterate through the number of terms 'n', and for each term, multiply the previous term by the common ratio 'r'. Print each term to display the geometric progression.

Pseudocode

function geometric_progression(a, ratio, n):
    Print "[ Start :", a, ", Ratio :", ratio, ", Size :", n, " ]"
    Initialize a loop variable 'i' to 0 and result as 'a'
    Loop until 'i' reaches 'n':
        Print the current 'result'
        Calculate the next term by multiplying 'result' with 'ratio'
        Increment 'i'

function main():
    geometric_progression(3, 2, 6)
    geometric_progression(2, 3, 10)

Algorithm Explanation

  1. The geometric_progression function takes three parameters: 'a', 'ratio', and 'n'.
  2. It prints the given values of 'a', 'ratio', and 'n' as information.
  3. It initializes a loop variable 'i' to 0 and a result variable to 'a'.
  4. The loop iterates from 0 to 'n-1':
    • It prints the current 'result'.
    • It calculates the next term by multiplying 'result' with 'ratio'.
    • It increments 'i'.
  5. The main function tests the geometric_progression function with different values.

Code Solution

// C program 
// Print the geometric progression
#include <stdio.h>

// Display geometric progression of given data
// a : starting point
// ratio : common ratio
// n : number of items
void geometric_progression(int a, int ratio, int n)
{
	printf("\n [ Start : %d, Ratio : %d , Size : %d ]\n Result  :", a, ratio, n);
	//Loop controlling variable
	int i = 0;
	int result = a;
	for (i = 0; i < n; i++)
	{
		printf("  %d", result);
		//Find next geometric progression
		result = result * ratio;
	}
	printf("\n");
}
int main()
{
	// Test Cases
	geometric_progression(3, 2, 6);
	geometric_progression(2, 3, 10);
	return 0;
}

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result  :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result  :  2  6  18  54  162  486  1458  4374  13122  39366
// Java program
// Print the geometric progression
class MyMath
{
	// Display geometric progression of given data
	// a : starting point
	// ratio : common ratio
	// n : number of items
	public void geometric_progression(int a, int ratio, int n)
	{
		System.out.print("\n [ Start : " + a + ", Ratio : " + ratio + " , Size : " + n + " ]\n Result :");
		//Loop controlling variable
		int i = 0;
		int result = a;
		for (i = 0; i < n; i++)
		{
			System.out.print("  " + result );
			//Find next geometric progression
			result = result * ratio;
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.geometric_progression(3, 2, 6);
		obj.geometric_progression(2, 3, 10);
	}
}

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
//Include header file
#include <iostream>
using namespace std;

// C++ program
// Print the geometric progression

class MyMath
{
	public:
		// Display geometric progression of given data
		// a : starting point
		// ratio : common ratio
		// n : number of items
		void geometric_progression(int a, int ratio, int n)
		{
			cout << "\n [ Start : " << a << ", Ratio : " << ratio << " , Size : " << n << " ]\n Result :";
			//Loop controlling variable
			int i = 0;
			int result = a;
			for (i = 0; i < n; i++)
			{
				cout << "  " << result;
				//Find next geometric progression
				result = result * ratio;
			}
			cout << "\n";
		}
};
int main()
{
	MyMath obj = MyMath();
	// Test Cases
	obj.geometric_progression(3, 2, 6);
	obj.geometric_progression(2, 3, 10);
	return 0;
}

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
//Include namespace system
using System;
// C# program
// Print the geometric progression
class MyMath
{
	// Display geometric progression of given data
	// a : starting point
	// ratio : common ratio
	// n : number of items
	public void geometric_progression(int a, int ratio, int n)
	{
		Console.Write("\n [ Start : " + a + ", Ratio : " + ratio + " , Size : " + n + " ]\n Result :");
		//Loop controlling variable
		int i = 0;
		int result = a;
		for (i = 0; i < n; i++)
		{
			Console.Write("  " + result);
			//Find next geometric progression
			result = result * ratio;
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.geometric_progression(3, 2, 6);
		obj.geometric_progression(2, 3, 10);
	}
}

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
<?php
// Php program
// Print the geometric progression
class MyMath
{
	// Display geometric progression of given data
	// a : starting point
	// ratio : common ratio
	// n : number of items
	public	function geometric_progression($a, $ratio, $n)
	{
		echo "\n [ Start : ". $a .", Ratio : ". $ratio ." , Size : ". $n ." ]\n Result :";
		//Loop controlling variable
		$i = 0;
		$result = $a;
		for ($i = 0; $i < $n; $i++)
		{
			echo "  ". $result;
			//Find next geometric progression
			$result = $result * $ratio;
		}
		echo "\n";
	}
}

function main()
{
	$obj = new MyMath();
	// Test Cases
	$obj->geometric_progression(3, 2, 6);
	$obj->geometric_progression(2, 3, 10);
}
main();

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
// Node Js program
// Print the geometric progression
class MyMath
{
	// Display geometric progression of given data
	// a : starting point
	// ratio : common ratio
	// n : number of items
	geometric_progression(a, ratio, n)
	{
		process.stdout.write("\n [ Start : " + a + ", Ratio : " + ratio + " , Size : " + n + " ]\n Result :");
		//Loop controlling variable
		var i = 0;
		var result = a;
		for (i = 0; i < n; i++)
		{
			process.stdout.write("  " + result);
			//Find next geometric progression
			result = result * ratio;
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var obj = new MyMath();
	// Test Cases
	obj.geometric_progression(3, 2, 6);
	obj.geometric_progression(2, 3, 10);
}
main();

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
#  Python 3 program
#  Print the geometric progression

class MyMath :
	#  Display geometric progression of given data
	#  a : starting point
	#  ratio : common ratio
	#  n : number of items
	def geometric_progression(self, a, ratio, n) :
		print("\n [ Start : ", a ,", Ratio : ", ratio ," , Size : ", n ," ]\n Result :", end = "")
		# Loop controlling variable
		i = 0
		result = a
		while (i < n) :
			print("  ", result, end = "")
			# Find next geometric progression
			result = result * ratio
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MyMath()
	#  Test Cases
	obj.geometric_progression(3, 2, 6)
	obj.geometric_progression(2, 3, 10)

if __name__ == "__main__": main()

Output

 [ Start :  3 , Ratio :  2  , Size :  6  ]
 Result :   3   6   12   24   48   96

 [ Start :  2 , Ratio :  3  , Size :  10  ]
 Result :   2   6   18   54   162   486   1458   4374   13122   39366
#  Ruby program
#  Print the geometric progression
class MyMath

	#  Display geometric progression of given data
	#  a : starting point
	#  ratio : common ratio
	#  n : number of items
	def geometric_progression(a, ratio, n)
	
		print("\n [ Start : ", a ,", Ratio : ", ratio ," , Size : ", n ," ]\n Result :")
		# Loop controlling variable
		i = 0
		result = a
		while (i < n)
		
			print("  ", result)
			# Find next geometric progression
			result = result * ratio
			i += 1
		end
		print("\n")
	end
end
def main()

	obj = MyMath.new()
	#  Test Cases
	obj.geometric_progression(3, 2, 6)
	obj.geometric_progression(2, 3, 10)
end
main()

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
// Scala program
// Print the geometric progression

class MyMath
{
	// Display geometric progression of given data
	// a : starting point
	// ratio : common ratio
	// n : number of items
	def geometric_progression(a: Int, ratio: Int, n: Int): Unit = {
		print("\n [ Start : " + a + ", Ratio : " + ratio + " , Size : " + n + " ]\n Result :");
		//Loop controlling variable
		var i: Int = 0;
		var result: Int = a;
		while (i < n)
		{
			print("  " + result);
			//Find next geometric progression
			result = result * ratio;
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		// Test Cases
		obj.geometric_progression(3, 2, 6);
		obj.geometric_progression(2, 3, 10);
	}
}

Output

 [ Start : 3, Ratio : 2 , Size : 6 ]
 Result :  3  6  12  24  48  96

 [ Start : 2, Ratio : 3 , Size : 10 ]
 Result :  2  6  18  54  162  486  1458  4374  13122  39366
// Swift program
// Print the geometric progression
class MyMath
{
	// Display geometric progression of given data
	// a : starting point
	// ratio : common ratio
	// n : number of items
	func geometric_progression(_ a: Int, _ ratio: Int, _ n: Int)
	{
		print("\n [ Start : ", a ,", Ratio : ", ratio ," , Size : ", n ," ]\n Result :", terminator: "");
		//Loop controlling variable
		var i: Int = 0;
		var result: Int = a;
		while (i < n)
		{
			print("  ", result, terminator: "");
			//Find next geometric progression
			result = result * ratio;
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	// Test Cases
	obj.geometric_progression(3, 2, 6);
	obj.geometric_progression(2, 3, 10);
}
main();

Output

 [ Start :  3 , Ratio :  2  , Size :  6  ]
 Result :   3   6   12   24   48   96

 [ Start :  2 , Ratio :  3  , Size :  10  ]
 Result :   2   6   18   54   162   486   1458   4374   13122   39366

Time Complexity

The time complexity of this code is linear, O(n), where 'n' is the number of terms. The loop iterates 'n' times, and each iteration involves a constant number of operations. The execution time grows linearly with the number of terms.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment