pta Exercise 2-17 Generate a power table of 3 (15 points)

Announcement of Zhejiang University Edition "C Language Programming (3rd Edition)" topic collection

Exercise 2-17 Generate a power table of 3 (15 points)

Input a non-negative integer n, generate a table of powers of 3, and output​0​​~3​n

The value of ​​. The power function can be called to calculate the power of 3.

Input format:

Input gives a non-negative integer n in one line.

Output format:

Output n+1 lines in the order of increasing power, each line format is "pow(3,i) = 3 to the power of i". The title guarantees that the output data does not exceed the range of a long integer.

Input sample:

3

Sample output:

pow(3,0) = 1
pow(3,1) = 3
pow(3,2) = 9
pow(3,3) = 27

Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    
    
    int n;
    int i,y;
    scanf("%d",&n);
    for (i=0;i<=n;i++){
    
    
            y=pow(3,i);
     printf("pow(3,%d) = %d\n",i,y);
    }
    return 0;
}

Submit result:

Insert picture description here

to sum up:

  1. In C language, the pow() function is used to find x to the y power. The x, y, and function values ​​are all double, and the syntax is "double pow(double x, double y)"; the parameter "double x" represents the base; the parameter "double y" represents the exponent.
  2. Pay attention to the output format, there are spaces around the equal sign.

Guess you like

Origin blog.csdn.net/crraxx/article/details/109131942