Thread: Quotient error

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    Quotient error

    Hello,

    I was trying to write a program that gives sum,product... of two integers, but, when i take ratio of two integers, it always gives "0" if number1 is less than number2.

    Why is that?
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > when i take ratio of two integers, it always gives "0" if number1 is less than number2.
    Without even looking at your code, I'm going to suggest that you have integer division which results in zero when the denominator is larger than the numerator.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    But i have made division variable in float. Even division is float, can't it have decimals when dividing two integers?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You misunderstood how the compiler seems the expression. In a nutshell, it goes like this:
    (int)/(int) = (int), then the result gets converted into float.

    You will have to convert at least one of the operands into float during the division, not after.
    Devoted my life to programming...

  5. #5
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Try adding a cast like this to your code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
        
        int n1;
        int n2;
        int sum;
        int diff;
        int pro;
        float quot;
        
        printf("Enter two integers as you like\n");
        
        scanf("%d %d", &n1, &n2);
        
        sum = n1+n2;
        diff = abs(n1-n2);
        pro = n1*n2 ;
        quot = (float)(n1)/(float)(n2) ;
        
        printf("Sum of them is \n%d \n",sum);
        printf("Difference of them is \n%d \n",diff);
        printf("Product of them is \n%d \n",pro);
        printf("Quotient of them is \n%.2f \n",quot);
        
        
        return 0 ;
        
    }

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Thank you both a lot. I got the idea now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  2. Dividing two floats not giving the quotient I need
    By Vespasian in forum C++ Programming
    Replies: 10
    Last Post: 01-10-2014, 08:33 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Keeping the fractional part of a quotient
    By jacobsh47 in forum C Programming
    Replies: 3
    Last Post: 03-17-2011, 11:02 AM
  5. 10 Quotient
    By stuart in forum C Programming
    Replies: 2
    Last Post: 11-19-2008, 11:20 PM

Tags for this Thread