Thread: A problem with percentage variable

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Exclamation A problem with percentage variable

    Why my output is still 0?
    Code:
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
    int marks;
    float percent;
    
    
    printf("\nEnter your marks (out of 1100): ");
    scanf("%d", &marks);
    percent=marks/1100*100;
    printf("\n\n%f", percent);
    
    getch();
    }
    OUTPUT:

    0

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > percent=marks/1100*100;
    You need to make the right hand side into a float expression before evaluating it (think of a cast).
    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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Moreover, take a look at Salem's avatar.

    You should write
    Code:
    int main(void)
    and not
    Code:
    void main()
    Also, it would be good to have a return 0; before main ends, like this
    Code:
    getch();
    return 0;
    }
    Then mind that Conio.h is non-standard, and thus I suggest you never use it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    percent=marks/1100*100;

    In C there is integer math and floating-point math and each operation happens between their respective types. If marks is an integer, say, 1099, then the first part does this

    1099 / 1100

    which gives 0 as a result because 0.99 is not an integer. On to the next part:

    0 * 100

    which gives 0 as a result.

    The safest way is to specify explicitly or implicitly that all members are type float.

    percent = (float)marks / 1100.0f * 100.0f

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    Thanks for help. It runs.....

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Hey shansajid.

    A couple more tips:
    Instead of using getch(), you can make a garbage variable and scan it in. This will put a prompt to the user so the program doesn't automatically exit.

    Another thing you can note is that if you are setting a float equal to some computation involving only integers, and you are performing division, you can always add ".0" to the end.
    For instance you have percent = marks/1100
    You can write percent = marks/1100.0 instead, and the float will store the decimal division instead of the integer division.

    EDIT: Before, I had in this post "Alternatively, you can use system("Pause"); at the end. Make sure you use #include <stdio.h>"
    This is a command in Windows only. If you are programming in a UNIX based system, this will not work. Also, it's not very efficient. It does pause the command line, though.
    Thanks for the correction post, std10093.
    Last edited by Derek Lake; 01-14-2013 at 10:49 AM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    NEVER USE system("pause") and similarly things

    //you are welcome
    Last edited by std10093; 01-14-2013 at 10:52 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're better off doing the multiply first and so doing it with all integer math:
    Code:
    percent = 100 * marks / 1100;
    But hey it's not really going to make any important difference here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wmi load percentage
    By ashaikh432 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2010, 05:42 AM
  2. Help with a percentage
    By Alecroy in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2010, 07:24 PM
  3. Percentage Complete?
    By Abda92 in forum C++ Programming
    Replies: 16
    Last Post: 09-21-2007, 03:04 PM
  4. Calculating a percentage
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2006, 10:24 AM
  5. Percentage
    By skyglin in forum C Programming
    Replies: 7
    Last Post: 06-23-2003, 05:18 PM