15,351,471 members
1.00/5 (1 vote)
See more:
```#include <stdio.h>

int summation(int N);

int summation(int N) {
if (N != 0)
return N + summation(N - 1);
else
return N;
}

int main(){
int N;
printf("\n Enter a positive number: ");
scanf("%d", &N);
printf("\n Sum = %d", summation(N));
printf("\n\n");
return 0;
}```

but not
`1 + 2 + 4 + 8 + 16 + …`

thats 1+2+3+4....n
what ı am

What I have tried:

ı try summution positive number of n
also
ı wanted 1+2+4+8+16+...n
not 1+2+3 ....n
what called ?
Posted
Updated 20-Jan-21 15:21pm

## Solution 1

your seqence is binary. 2^0, 2^1, 2^2, etc. So you can do what by passing in the powers and doing 2 the power of x in your code

## Solution 2

```#include <bits/stdc++.h>
using namespace std;

// function to calculate sum of series
int calculateSum(int n)
{
// initialize sum as 0
int sum = 0;

// loop to calculate sum of series
for (int i = 0; i < n; i++) {

// calculate 2^i
// and add it to sum

sum = sum + (1 << i);
}
return sum;
}

// Driver code
int main()
{
int n = 10;
cout << "Sum of series of power of 2 is : "
<< calculateSum(n);
} ```

how do convert C ı dont know c++ .
Christian Graus 20-Jan-21 21:45pm

Every line of C is valid C++