Print the given geometric progression
Here given code implementation process.
// 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
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