Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
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

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
 
Share this answer
 
#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++ .
 
Share this answer
 
Comments
Christian Graus 20-Jan-21 21:45pm    
Every line of C is valid C++

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900