Problem with a simple Arithmetic equation.

This is a discussion on Problem with a simple Arithmetic equation. within the C Programming forums, part of the General Programming Boards category; Greeting EveryOne Why am i always getting 0 as a result in the following code? Code: #include <stdio.h> int CelsiusToFehrenheit(int ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    Problem with a simple Arithmetic equation.

    Greeting EveryOne

    Why am i always getting 0 as a result in the following code?

    Code:
    #include <stdio.h>
    
    int CelsiusToFehrenheit(int F);
    
    int main()
    {
        int i;
        int j;
    
        j = (5/9)*(40-32);
    
        printf("\n");
        printf("40F = %dC\n\n", j);
        printf("100F = %dC\n\n",CelsiusToFehrenheit(100));
    
        printf("Celsius\t\t\tFehrenheit\n");
    
    
    
        for (i=1; i<=10; i++)
        {
            printf("%d\t\t\t%d\n", i, CelsiusToFehrenheit(i));
        }
    
        printf("\n");
        return 0;
    }
    
    
    int CelsiusToFehrenheit(int F)
    {
        int C;
        C = (5/9)*(F-32);
        return C;
    }
    Thanks In Advance
    Last edited by Laythe; 02-26-2011 at 07:09 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is 5 / 9 ? 0.
    What's 0 * anything? 0.

    If you want floating point numbers, you need floating point variables.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Thank you quzah for your hint, i have made it

    Code:
    #include <stdio.h>
    
    float CelsiusToFehrenheit(float F);
    
    int main()
    {
        float i;
        float j;
    
        j = (5.0/9)*(40-32);
    
        printf("\n");
        printf("40F = %.2fC\n\n", j);
        printf("100F = %.2fC\n\n",CelsiusToFehrenheit(100.0));
    
        printf("Celsius\t\t\tFehrenheit\n");
    
    
    
        for (i=1; i<=10; i++)
        {
            printf("%.0f\t\t\t%.2f\n", i, CelsiusToFehrenheit(i));
        }
    
        printf("\n");
        return 0;
    }
    
    
    float CelsiusToFehrenheit(float F)
    {
        float C;
        C = (5.0/9)*(F-32);
        return C;
    }
    any other tips?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Should your temperatures be reported in tenths or hundredths of a degree? I'm used to seeing them in just integers.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Should your temperatures be reported in tenths or hundredths of a degree? I'm used to seeing them in just integers.
    The exercise was tended to practice the printf function and how it handles ints and floats.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I would print them in tenths.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with piping in my simple UNIX shell
    By Integral Birth in forum C Programming
    Replies: 0
    Last Post: 11-28-2010, 07:37 PM
  2. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-17-2010, 12:00 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21