我写了一个程序来计算一个数字的第n个根,最多可以保留2位小数。例如81的第4个根是3. 125的第3个根是5.它除了4的第2个根以外都很好地工作。它给出输出1.99而不是2.这里是代码。编号的第n个根(
#include<stdio.h>
int main(int argc, char **argv)
{
double root1(int,int);
int n;
int num1;
double root;
printf("\n\n-----------This is the programme to find the nth root of a number-----------\n\n");
printf("Enter a nuber greater then 1 : ");
scanf("%d",&num1);
if(num1>1)
{
printf("Enter the value for 'n'(the root to be calculated) : ");
scanf("%d",&n);
root = root1(num1,n);
printf("%d th Root of %d is %f\n\n", n,num1,root);
}
else
printf("wrong entry");
return 0;
}
double root1(int a, int b)
{
int j;
double i,k;
double incre = 0.01;
for(i=1; i<=a; i = i+incre)
{
for(j=0;j<b;j++)
{
k=k*i;
}
if(a<k)
{
return(i-incre);
break;
}
else
k=1;
}
}
我试了几个小时,但不能纠正它。 任何人都可以调试?我会非常感激。
请使用代码按钮:它可以帮助他人阅读您的代码。 – 2010-08-16 15:16:13
抱歉给您带来不便。我刚来这地方。下次我会照顾这个。 – narayanpatra 2010-08-16 15:17:24
ques编辑。它不是3,而是高达2位小数。 – narayanpatra 2010-08-16 15:18:30