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

CServer Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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
Updated on 09-Aug-2019 11:40:23