Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top