You can also read about dynamic array in c and C Static Function.
Algorithm for Sum of Geometric Progression Series
- Start
- Declare the variables that depict the first term of the series, the total number of terms, and the common ratio for the geometric series.
- Use a for loop that will calculate the sum.
- Declare two variables for sum and element.
- Update both the elements in each iteration
- At the end display the calculated sum.
- Stop
Code:
// C Program to find the Sum of Geometric Progression Series
#include <stdio.h>
#include<math.h>
int main ()
{
// First number of gp series;
int a = 2;
// Common ratio for the gp series
int r = 2;
// Total number of terms in the gp series
int n = 10;
// This will store the total sum of Geometric Progression Series
float sum = 0;
// Variable for iteration in the loop
int i ;
// Variable to update the gp term initially it will be first term
int var = a;
// Print the series
printf ("The G.P Series is : ");
for (i = 0; i < n; i++)
{
printf ("%d ", var);
//Update the sum in each iteration
sum = sum + var;
//Update the term in each iteration
var = var * r;
}
//Print the sum
printf ("\nThe Sum for Geometric Progression Series = %f\n", sum);
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output:
The G.P Series is : 2 4 8 16 32 64 128 256 512 1024
The Sum for Geometric Progression Series = 2046.000000
You can also try this code with Online C Compiler
Run Code
Time Complexity
O(N)
The time complexity to find the sum of Geometric Progression Series is O(N), where 'N' is the total number of terms in the series.Since we are using a loop for iteration on all the terms to print the sum of the Geometric Progression Series, it will cost linear time complexity.
Read More - Time Complexity of Sorting Algorithms
Space Complexity
O(1)
The space complexity to print the sum of the Geometric Progression Series is O(1), Since we have not used any extra data structure to store the elements and neither there are recursive calls so the space complexity for the above solution is constant.
Also read, Tribonacci Series and Short int in C Programming
FAQs
-
What are real-life applications of the sequence and series?
Ans: Arithmetic sequences are used in everyday life for various reasons, including assessing the capacity of an auditorium, calculating predicted revenues from working for a firm, and constructing wood heaps using stacks of logs. Arithmetic sequences are algebra and geometry tools that aid mathematicians and others in problem-solving.
-
What is the geometric mean of two numbers?
Ans: Geometric Mean is the average of two numbers in a geometric sequence. If x and y are the sequence's two numbers, then the geometric mean is GM = √xy.
Key Takeaways
In this article, we have extensively discussed the solution to print Sum of Geometric Progression Series in c along with its time and space complexity.
Check out this problem - Two Sum Problem
Also read reverse a number.
Until then, All the best for your future endeavours, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding this problem, and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!”