simple question about %

This is a discussion on simple question about % within the C Programming forums, part of the General Programming Boards category; Hi, how would you code finding the percentage of one number and assigning that value to a variable. Like finding ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    simple question about %

    Hi,
    how would you code finding the percentage of one number and assigning that value to a variable. Like finding 15% of 150 and assigning that to
    Code:
    int percent;
    I think you have to write it as 0.15, not 15% ? ?

    thanks
    Last edited by richdb; 01-20-2006 at 05:43 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't you know how to turn a fraction into a percentage? This is fairly basic math.


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

  3. #3
    old man
    Join Date
    Dec 2005
    Posts
    90
    Once you figure out the math, perhaps you'll notice that type "int" won't really cut it.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Im just not sure how to code it.

  5. #5
    old man
    Join Date
    Dec 2005
    Posts
    90
    Make your best effort, get it to compile, and post your code.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Code:
    #include<stdio.h>
    
    int value;
    float percentage,output;
    
    value = 150;
    percentage = 0.15;
    
    int main()
    {
    	output = value * percentage;
    	printf("\n Result = %.2f \n", output);
    }
    Would something like that work ?

    I get an output but I also get a compile error, something about having to truncate a double to a floating point.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,344
    This is primary school maths. If you want to find 15% of X, multiple X by 15 and then divide the result of that by 100.

    In C, as with a lot of programming languages, if you do it with integer variables, rounding will occur. For example, 15% of 50 is actually 7.5. But if the value of 50 is of type int, the result will be 7 (7.5 can not be represented as an integer).

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    As for your compiler error . . .
    Code:
    #include<stdio.h>
    
    int value;
    float percentage,output;
    
    value = 150;
    percentage = 0.15;
    
    int main()
    {
    	output = value * percentage;
    	printf("\n Result = %.2f \n", output);
    }
    You can't have executable code outside of a function (at least not that kind). Change it to this:
    Code:
    #include<stdio.h>
    
    int value;
    float percentage,output;
    
    int main()
    {
        value = 150;
        percentage = 0.15;
    	output = value * percentage;
    	printf("\n Result = %.2f \n", output);
    }
    Or, better yet, use local variables:
    Code:
    #include<stdio.h>
    
    int main()
    {
        int value;
        float percentage,output;
    
        value = 150;
        percentage = 0.15;
    
    	output = value * percentage;
    	printf("\n Result = %.2f \n", output);
    }
    Or, best of all, use initialization instead of assignment:
    Code:
    #include<stdio.h>
    
    int main()
    {
        int value = 150;
        float percentage = 0.15,output;
    
    	output = value * percentage;
    	printf("\n Result = %.2f \n", output);
    
        return 0;  /* remember to include this, too */
    }
    Of course, this doesn't fix your math, just your compiler errors.

    [edit]
    This is primary school maths. If you want to find 15% of X, multiple X by 15 and then divide the result of that by 100.
    Or better yet, multply X by .15, which is what [s]he's doing.

    In C, as with a lot of programming languages, if you do it with integer variables, rounding will occur. For example, 15% of 50 is actually 7.5. But if the value of 50 is of type int, the result will be 7 (7.5 can not be represented as an integer).
    This is irrelevent. The result is stored in a float (which would be better of as a double, but still).

    I think (his|her) math is fine.
    [/edit]
    Last edited by dwks; 01-21-2006 at 02:24 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Quote Originally Posted by dwks
    Of course, this doesn't fix your math, just your compiler errors.
    Thanks for fixing my compile error, thats going to come in handy for my course when/if I come accross that problem again I will ken how to fix it.

    As for my maths, I`m correct in multiplying by the decimal of the percentage value I`m looking for. Not sure what you mean by my math problem?

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Ok, I was a little bored and this kind of interested me [ seeing as I`m doing a C programming course it is something for me to practise on ].

    Had a little play around and came up with a very simple percentage calculator that does the maths for the user, all they have to do is punch in numbers.

    PHP Code:
    #include<stdio.h>

    /*create variables*/
     
    float valuepercentageresult;
     
    int main()
    {
        
    /*Give user some idea of what it is they are using*/
        
    printf("\t\t**SIMPLE PERCENTAGE VALUE CALCULATOR**\n");
        
    /*Inform user how to enter data*/
        
    printf("\nPlease enter the values you wish to be calculated in the following format:");
        
    printf("\n[VALUE], [PERCENTAGE]\n");
        
    /*Capture data*/
        
    scanf_s("%f,%f", &value, &percentage);
        
        
    /*Do the maths*/
        
    result value * (percentage/100);
        
    /*Produce a result*/
        
    printf("\n Result = %.2f \n"result);

    I put the values as floating point numbers just incase the user has some wierd and/or wonderful numbers to work with. Not sure if I have set up the variables correctly but I didn't get any compile errors so I am assuming my code is fine. Perhaps not formatted like a pro, but I`m not a pro so I`m not going to worry yet.

    Is there anything else that could be done to it ? Perhaps a switch statement so the user can exit without using the cross window buttons ?

    Sorry if I have taken over this thread by the way, I ken its not mine, just kinda interested me and appear to be going in the right direction with what the original poster wanted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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