having problem storing a number the program gives us a compl

having problem storing a number, the program gives us a completley wrong answer compared to what the calculators give us given that the program gives us the wrong answer, probably because its not storing the whole number because its too big

Answer should be ; Aproximatelly 3.990454x10^14 but the program gives us 553402642

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{

long long periodTime;
const double UNIVERSAL_GRAVITATION = 0.00000000006673;
const long long int EARTH_MASS = 5980000000000000000000000;

periodTime = (UNIVERSAL_GRAVITATION * EARTH_MASS);


cout << periodTime << endl;


return 0;
}

Solution

#include <iostream>
#include <cmath>

//Earth mass is too large for supporting in a single variable

#include <iomanip>
#include <cctype>
using namespace std;
int main()
{

long double periodTime;
const long double UNIVERSAL_GRAVITATION = 0.00000000006673;
const long double EARTH_MASS = 598.0;
const int EARTH_MASS_EXP = 22;
int c=0;
periodTime = (UNIVERSAL_GRAVITATION * EARTH_MASS);
for(int i=1;i<=EARTH_MASS_EXP;i++)
{
while(periodTime<1){
periodTime*=10.0;
c++;
}

}



cout << periodTime <<\"+e^\"<<(EARTH_MASS_EXP-c)<< endl;

return 0;
}

having problem storing a number, the program gives us a completley wrong answer compared to what the calculators give us given that the program gives us the wro
having problem storing a number, the program gives us a completley wrong answer compared to what the calculators give us given that the program gives us the wro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site