#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    No error but giving wrong solution HELP PLEASE


    CHECK

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a;
    float c;
    c=a*(9/5)+32;
    printf("===============================================================================");
    printf("\n \n \t This Program will Convert Centigrade Temperature to Fahrenheit ");
    printf("\n \n================================================================================");
    printf("\n \n Enter the Temperature In Centigrade : ");
    scanf("%d",&a);
    printf("\n %d Centigrade = %f Fahrenheit",a,c);
    printf("\n \n *******************************************************************************");
    printf("\n \t \t This Program is Developed By Raja Muhammad Tahir Qaiser \n \n \t \t \t Roll Number 151 BS IT 1st Semester ");
    printf("\n \n *******************************************************************************");
    getch();
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    1. Use tags when posting code, so it looks like this.
    Code:
    #include <stdio.h>
    /* yes, really!, main returns an int (not void) */
    int main ( ) {
        printf("Hello world\n");
        return 0;
    }
    2. Check your statement order.
    c=a*(9/5)+32;
    ....
    scanf("%d",&a);
    printf("\n %d Centigrade = %f Fahrenheit",a,c);


    3. Be mindful that 9/5 is done in integer arithmetic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by salem
    3. Be mindful that 9/5 is done in integer arithmetic.
    Raja, that is a deceptively simple statement, but if you choose to ignore it then you will have to endure many hours of frustration.

    You want 9/5 to give you a float value of 1.8, just as your electronic calculator would do. That's what's called floating-point division (that "floating-point" is where the float datatype got its name from).

    But C also does integer division, which is the kind that we were first taught in school, where you get an integer (AKA "whole number") quotient and a remainder. So the integer division result of 9/5 would be 1 R 4. By the way and for your information, you use the modulo operator, %, to get the remainder of an integer division.

    Multiplying by the floating-point value 1.8, your result would be 1.8 times greater, a little less than twice the original, but multiplying by the integer division result of 1, the result would be equal to the original value. Quite obviously, you want the floating-point division result and not the integer division one.

    C uses the same symbol, / , for both integer and floating-point division, so then how does C know which kind of division you want it to use? Quite simply through the datatypes of the operands you give it. If both operands (in this case, 9 and 5) are integer, then it's integer division, but if either if them are floating-point then it's floating-point division. At this point, you need to review the section of your textbook on literals, specifically regarding the difference between int and float and double literals. For example, 42 would be int, whereas 42.0 would be double.

    BTW, the same thing applies to multiplication ( * ), but in most situations it's difficult to detect the difference that it makes.

IMN logo majestic logo threadwatch logo seochat tools logo