문제

I tried to write Series below in C:
series : (1^1),(2^(1/2)),(6^(1/4)),(24^(1/8)),...,((n!)^((1/2)^n)).

C Code:

#include <stdio.h>
#include <math.h>


int fact(int x){
  if (x==1)
    return 1;
    else return x*fact(x-1);
}


int main(){
    int x,y;
    scanf("%d",&x);
    y=x;
    x=fact(x);
    y=pow(0.5,y-1);
    double h;
    h=pow(x,y);
    printf("\n%lf" ,h);
    return 0;
}

Why it's print 1.00000 all the time?

도움이 되었습니까?

해결책

Because the variable y is int, so storing the return value of pow() in it truncates. Read the manual page for pow().

Please check how you enable all warnings in your compiler, and look at the compiler output.

다른 팁

Function pow(a,b) takes double as argument and returns double. Try to replace int with double.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top