Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I don't get floating point number when i divide (float) number by (float) number?

I had a function (money) needs two parameters (salary, days)
every week there are 2 holidays so working days = days – (weeks * 2)
you only spend money on working days
i want to calculate how much money do you spend per day
so i did –> salary / working days = sometimes this must equal floating point number but i don’t get it

#include <iostream>
using namespace std;

int money(float salary, int days)
{
    // every week you have 2 days off
    // you only spend money on working days
    float workingdays = days - ( (days / 7) * 2 );
    return salary / workingdays; 
}

int main()
{
    cout << money(2015, 21) << "\n"; // 134.333
    cout << money(4500, 40) << "\n"; // 150
    return 0;
}

in the first call i need the function money to return 134.33333333 but it returns 134
why??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because in the money function you are returning int so the function return int value .Modify int with float , then your money function return float value

`

float money(float salary, int days){
    float workingdays = days - ( (days / 7) * 2 );
    return salary / workingdays;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading