C Programming for Sum of sequence 2, 22, 222, ………


Given is a sequence: 2,22,222,2222….. and we need to find the sum of this sequence. So we have to Go for the mathematical formula that is made to find the sum of the series,

The explanation of the formula goes in such a way that -

sum =[2+22+222+2222….]
sum= 2*[1+11+111+1111….]
Sum = 2/9[9+99+999+9999….]
sum= 2/9 [10+100+1000+10000+.....]
sum = 2/9[10+102+103+104+.....]
sum=2/9*[(10n-1-9n)/9]

Example

#include <stdio.h>
#include <math.h>
int main() {
   int n = 3;
   float sum = 2*(pow(10, n) - 1 - (9 * n))/81;
   printf("sum is %d", sum);
   return 0;
}

Output

sum is 879
raja
Published on 09-Aug-2019 11:40:23
Advertisements