笔记1 用程序就算一个数x的n次方(其中n为非负整数)

#include <stdio.h>
void main()
{
    int count, n;
    float x, y;
    printf("Enter the values of x and n: \n");
    scanf_s("%f %d",&x,&n);
    y = 1.0;
    count = 1;  /*Initialisantion*/
    /*loop begins*/
    while(count <= n)/*teasting*/
    {
        y = y*x;
        count++; /*incrementing*/
    }/*loop ends*/
    printf("\nx = %f; n = %d; x to power n = %f\n",x,n,y);
}

/*用for语句*/
#include <stdio.h>
void main()
{
    int count, n;
    float x, y;
    printf("Enter the values of x and n: \n");
    scanf_s("%f %d", &x,&n);
    y = 1.0;
    for(count=1;count<=n;count++)/*loop begins*/
    {
        y = y*x;
    }/*loop ends*/
    printf("\nx = %f; n = %d; x to power n = %f\n",x,n,y);
}
相关文章
相关标签/搜索