Ç - 写作的指数函数,而无需使用战俘 [英] C - Writing a function for an exponent without using Pow

查看:154
本文介绍了Ç - 写作的指数函数,而无需使用战俘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序:

#include <stdio.h>

long int x_to_the_n (int x,int n)
{
    int i; 
    int number;


 int i; 
int i = 1;

for (i = 0; i < n; ++i)
    x = x*x;

return(number);
}

int main()
{
    int number;
    int exponent;
    int answer;

    printf  ("Enter a number: ");
    scanf ("%i", &number);

    printf  ("Enter a number that represents the power you want your number to be raised     to: ");
    scanf ("%i", &exponent);

    answer = x_to_the_n(number,exponent); 

    printf("X To The N is %li",answer);

    return 0;
}

目前,该功能x_to_the_n不正确等同X ^ñ...我想知道如果任何人有建议,以计算x ^到N不使用的C战俘库函数。

Right now, the function x_to_the_n is not correctly equating x ^ n...I was wondering if anyone had suggestions to calculate x to the n without using the c pow library function.

推荐答案

试试这个:

long int x_to_the_n (int x,int n)
{
    int i; /* Variable used in loop counter */
    int number = 1;

    for (i = 0; i < n; ++i)
        number *= x;

    return(number);
}

这篇关于Ç - 写作的指数函数,而无需使用战俘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆