there's something wrong.. :/

Discussion in 'C' started by jam143, Jul 2, 2011.

  1. jam143

    jam143 New Member

    Joined:
    Jul 1, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    n/a
    Location:
    philippines
    Hello, I think there's something wrong in my code.
    I made a program converting °C-°F .
    the formula is °F = (1.8*°C) + 32

    Here's the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int C;
        int F;
        printf("Enter the value of °C: ");
    
        scanf("%d", &C);
    
        total = (1.8 * C) + 32;
    
        printf("°F=%d", F);
    
        getch();
    
    }
    
    when I enter the value of C as 2. the answer should be 35.6 .. but it only shows 35.. it doesn't have any decimals.. what should I do?? thank you in advance if someone will help.. :)
     
  2. jam143

    jam143 New Member

    Joined:
    Jul 1, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    n/a
    Location:
    philippines
    ahh no.. it's wrong.. here's the correct one..

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int C;
        int F;
        printf("Enter the value of °C: ");
    
        scanf("%d", &C);
    
        F = (1.8 * C) + 32;
    
        printf("°F=%d", F);
    
        getch();
    
    }
    
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    TO get the output in decimals we need to use the 'float' data-type .

    Corrected Code :-

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    int C;
    [COLOR="Red"]float F;[/COLOR]
    printf("Enter the value of °C: ");
    
    scanf("%d", &C);
    
    F = (1.8 * C) + 32;
    
    [COLOR="Red"]printf("°F=%f", F);[/COLOR]
    
    getch();
    
    }
    
    
    Output :-

    Code:
    Enter the value of C: 2
    F=35.599998
    
     
  4. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    what u(jam 143) have writen is absolutely correct but...............
    there is only one controversy that your answer will not be printed in decimals
    because int range is-32768to 32767 so it cannot accept decimal values
    so we use other data type called "float" whose range is -3.4e-38 to -3.4e+38
    so we can use it

    just print the answer as printf("F=%f",f);
    you will get the required answer
     
  5. jam143

    jam143 New Member

    Joined:
    Jul 1, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    n/a
    Location:
    philippines
    thank you for help.. for the both of you.. really big help.. thank you so much.. :))
     
  6. kupparamakrishna

    kupparamakrishna New Member

    Joined:
    Jun 23, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    banglore
    Iam sorry if am wrong about the above program how can int f;
    give you floating point value if u write %f.
     
  7. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    yes sir,
    it will show abnormal termination
    what i mean in before post is to cast the data type
    for example x=(float)y;
     
  8. kupparamakrishna

    kupparamakrishna New Member

    Joined:
    Jun 23, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    banglore
    ok ok thats fine

    HI I AM RAMAKRISHNA
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice