C PROGRAM ON PROGRESSION SERIES
C program to Find the Sum of Arithmetic Progression Series
/*
* C Program to Find the Sum of A.P Series
*/
#include <stdio.h>
#include <math.h>
int main()
{
int a, d, n, i, tn;
int sum = 0;
printf("Enter the first term value of the A.P. series: ");
scanf("%d", &a);
printf("Enter the total numbers in the A.P. series: ");
scanf("%d", &n);
printf("Enter the common difference of A.P. series: ");
scanf("%d", &d);
sum = (n * (2 * a + (n - 1)* d ))/ 2;
tn = a + (n - 1) * d;
printf("Sum of the A.P series is: ");
for (i = a; i <= tn; i = i + d )
{
if (i != tn)
printf("%d + ", i);
else
printf("%d = %d ", i, sum);
}
return 0;
}
Runtime Test Cases
Output:
$cc pgm21.c
$ a.out
Enter the first term value of the A.P. series: 1
Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 2
Sum of the A.P series is: 1 + 3 + 5 + 7 + 9 = 25
C program to Find the Sum of Geometric Progression Series
/*
* C Program to Find the Sum of G.P Series
*/
#include <stdio.h>
#include <math.h>
int main()
{
float a, r, i, last_term, sum = 0;
int n;
printf("Enter the first term of the G.P. series: ");
scanf("%f", &a);
printf("Enter the total numbers in the G.P. series: ");
scanf("%d", &n);
printf("Enter the common ratio of G.P. series: ");
scanf("%f", &r);
sum = (a *(1 - pow(r, n + 1))) / (1 - r);
last_term = a * pow(r, n - 1);
printf("last_term term of G.P.: %f", last_term);
printf("\n Sum of the G.P.: %f", sum);
return 0;
}
Runtime Test Cases
Output:
$ cc pgm22.c -lm
$ a.out
Enter the first term of the G.P. series: 3
Enter the total numbers in the G.P. series: 7
Enter the common ratio of G.P. series: 2
last_term term of G.P.: 192.000000
Sum of the G.P.: 765.000000
C Program to Find Sum of Series 1^2 + 2^2 + …. + n^2
/*
* C Program to find the sum of series 1^2 + 2^2 + …. + n^2.
*/
#include <stdio.h>
int main()
{
int number, i;
int sum = 0;
printf("Enter maximum values of series number: ");
scanf("%d", &number);
sum = (number * (number + 1) * (2 * number + 1 )) / 6;
printf("Sum of the above given series : ");
for (i = 1; i <= number; i++)
{
if (i != number)
printf("%d^2 + ", i);
else
printf("%d^2 = %d ", i, sum);
}
return 0;
}
Runtime Test Cases
Output:
$ cc pgm18.c
$ a.out
Enter maximum values of series number: 4
Sum of the above given series : 1^2 + 2^2 + 3^2 + 4^2 = 30
C Program to Find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!
/*
* C Program to Find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!
*/
#include <stdio.h>
double sumseries(double);
main()
{
double number,sum;
printf("\n Enter the value: ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\n Sum of the above series = %lf ", sum);
}
double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 +(i / f);
}
return(sum2);
}
Runtime Test Cases
Output:
$ cc pgm20.c
$ a.out
Enter the value: 5
Sum of the above series = 2.708333