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