1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Basics of C programming

Discussion in 'C' started by vivekgupta, Oct 25, 2012.

  1. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I wrote code for sinx

    But it is showing output as INF

    Why INF?? what's wrong with code ....
    plz help
    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
    float x,t=0,sum;
    int i,n,sig=1;
    printf("Input X,N");
    scanf("%f %d",&x,&n);
    x*=(3.14/180);
    sum=x;
    for(i=0;i<n;i++)
    {
    sig*=-1;
    t=(sig*x*x)/(2*i*(2*i+1));
    sum+=t;
    }
    printf("SIN(X) = %f\n",sum);
    return 0;
    }
    :snore:
     
    Last edited by a moderator: Oct 25, 2012
  2. sirama

    sirama New Member

    Joined:
    Jun 18, 2012
    Messages:
    7
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Systems Er.
    Home Page:
    In you code you are using the for loop that starts from zero.
    Code:
    for(i=0;i<n;i++)
    So when the loop runs for the first time, you get infinite in the variable t and you sum become infinite.

    Code:
    t=(sig*x*x)/(2*i*(2*i+1));z
    Whatever you add to Infinite, the result will be infinite

    Start your loop from 1 (ie for(i=1;i<n;i++)
     
    vivekgupta likes this.

Share This Page