我寫了一個程序來計算一個數字的第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