zoukankan      html  css  js  c++  java
  • c语言中自定义函数计算x的n次方

    c语言中自定义函数计算x的n次方。

    1、直接输出形式

    #include <stdio.h>
    
    int main(void)
    {
        int i, x, n;
        int tmp = 1;
        puts("please input the values of x and n.");
        printf("x = "); scanf("%d", &x);
        printf("n = "); scanf("%d", &n);
        
        for(i = 1; i <= n; i++)
        {
            tmp *= x;
        }
        
        printf("the result ls: %d\n", tmp);
        return 0;
    }

    2、自定义函数,通用浮点型和整型

    #include <stdio.h>
    
    double power(double x, int n)
    {
        double tmp = 1.0;
        int i;
        for(i = 1; i <= n; i++)
        {
            tmp *= x;
        }
        return tmp;
    }
    
    int main(void)
    {
        double a; 
        int b;
        puts("please input double a and int b.");
        printf("a = "); scanf("%lf", &a);
        printf("b = "); scanf("%d", &b);
        
        printf("result: %.2f\n", power(a, b));
        return 0;
    }

    3、

    #include <stdio.h>
    
    double power(double x, int n)
    {
        double tmp = 1.0;
        
        while(n-- > 0)
        {
            tmp *= x;
        }
        return tmp;
    }
    
    int main(void)
    {
        double a;
        int b;
        puts("please input double a and int b.");
        printf("a = "); scanf("%lf", &a);
        printf("b = "); scanf("%d", &b);
        
        printf("result: %.2f\n", power(a, b));
        return 0;
    }

  • 相关阅读:
    铁大电梯调度需求分析
    结对开发2
    四则运算3
    团队开发——冲刺2.d
    团队开发——冲刺2.c
    团队开发——冲刺2.b
    团队开发——冲刺2.a
    团队开发——冲刺1.g
    团队开发——冲刺1.f
    团队开发——冲刺1.e
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14736490.html
Copyright © 2011-2022 走看看