Casting

I have to create a small program which takes world population and a nations population as long long int. Then I have to divide and figure out the percentage of population but when I try to cast it it does not seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    long long world, nation;
    double p;
    cout << "enter world population: ";
    cin >> world;
    cout<< "enter nation population: ";
    cin >> nation;
    p = (static_cast<double> (nation)/static_cast<double> (world))*100;
    cout << "the population of the US is " << p << " of the world population.";
    return 0;
}


Never mind figured it out. I was casting it after the calculation was done in int which would lose all decimal values.
Last edited on
Topic archived. No new replies allowed.