Experiment 2-4-6 Finding the sum of powers (15 points)

This question requires writing a program to calculate
sum = 2 ​ 1 + 2 2 + 2 3 +… + 2 n sum=2^{​1}+2^{2}+2^{3}+...+2^{n}sum=21+22+23++2n
​​ can be exponentiated by calling the pow function.

Input format:

Enter a positive integer in one line n(≤10).

Output format:

result = 计算结果Output in the format " ".

Input sample:

5

Sample output:

result = 62

Code:

# include <stdio.h>
# include <stdlib.h>
# include <math.h>

int main() {
    
    
    int n,result = 0,i;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
    
    
        result += pow(2,i);
    }
    printf("result = %d",result);
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

Nothing to say, basic operation!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114434317