C Examples

WAP in C that uses a function that can compute sum of any geometric series

The Program in C that uses a function that can compute sum of any geometric series is given below:

#include <stdio.h>

double geometric_series_sum(double a, double r, int n) {
    // Function to calculate the sum of a geometric series
    return a * (1 - pow(r, n)) / (1 - r);
}

int main() {
    double a, r;
    int n;

    printf("Enter the first term of the series: ");
    scanf("%lf", &a);
    printf("Enter the common ratio of the series: ");
    scanf("%lf", &r);
    printf("Enter the number of terms in the series: ");
    scanf("%d", &n);

    printf("The sum of the geometric series is: %lf", geometric_series_sum(a, r, n));
    return 0;
}

Output:

Enter the first term of the series: 4
Enter the common ratio of the series: 51
Enter the number of terms in the series: 6
The sum of the geometric series is: 1407703024.000000

Pro-Tips💡

This program uses the same formula as before to calculate the sum of the geometric series but in this version, I have removed the unnecessary variable double sum and the return statement is added in the function itself.

Also, I have removed the declaration of sum inside the main function and the function is called inside the printf statement.

This will make the code more concise and easier to read.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X