In my program I need to multiply a fraction (which I have done) and now I need to print it out in a percent. When I run it all I get is the Fraction and no percent after it. I'm using the formula
(Numerator/Denominator)*100= a percent
Could someone tell me what I'm doing wrong and how to fix it. Thank you.
I also know I'm going to get called out for using system("pause")but that is the only pause function that is currently working for me.
double Final = (100.0 * MultiplicationResult1) / MultiplicationResult2;
The 100.0 persuades the compiler to use double arithmatic. In your code, the caculation uses ints and then the result is converted to double when it's assigned to the variable Final.
In addition to the usual rules of arithmatic, you've got to be aware of how C++ treats, and promotes, types.
Andy, is the calculation from int to double reconized by C++?
Also I tried what you said and I got the same thing that I was currently getting which was just the fraction answer and it never showed the output
1 2 3
cout<<"The percentage is "<<Final<<"%";
cout<<"This program will multiply fractions" <<endl;
From a mathematical point of view it doesn't matter which order you do things as multiplication and division are commutative.
From a type conversion point of view, I know that 100.0 * MultiplicationResult1 is going to be a double, even though MultiplicationResult1 is an int. So I'm doing the multiplication first to force the type promotions. As the result of that is a double, the division will also be done using double arithmetic.
If you want, you can use a cast instead. I just prefer the form above.
double Final = (static_cast<double>(MultiplicationResult1) / multiplicationResult2) * 100;
(you can also cast MultiplicationResult2 if you want to, but it's not necessary)
Of course, you can just do without the ()s, if you want.
Andy
PS For this kind of calculation, it can be better to multiply and then divide. Here you get an OK approx. for the percentage even with integer maths.
OK I tried that but still the output is the same. I just doesn't show the output. What I want to show is (The percent is [eg.57]%). The current output is everything but that one line.
#include <iostream>
usingnamespace std;
void MultiplyNumbers ()
{
// not sure what this does?
}
int main() // MUST return int
{
....
// do work here
MultiplyNumbers ();
return 0;
}
or is it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
void MultiplyNumbers ()
{
// do work here
}
int main() // MUST return int
{
...
MultiplyNumbers ();
return 0;
}
#include <iostream>
usingnamespace std;
void MultiplyNumbers()
{
cout<<"Enter the First numerator: ";
int FirstNumerator = 0;
cin>>FirstNumerator;
cout<<"Enter the First denominator: ";
int FirstDenominator = 0;
cin>>FirstDenominator;
cout<<"Enter the Second numerator: ";
int SecondNumerator = 0;
cin>>SecondNumerator;
cout<<"Enter the Second denominator: ";
int SecondDenominator = 0;
cin>>SecondDenominator;
int MultiplicationResult1 = FirstNumerator * SecondNumerator;
int MultiplicationResult2 = FirstDenominator * SecondDenominator;
cout<< FirstNumerator << " x " <<SecondNumerator;
cout<< " = " <<MultiplicationResult1 << endl;
cout<<" --"<<endl;
cout<< FirstDenominator << " x " <<SecondDenominator;
cout<< " = " <<MultiplicationResult2 << endl;
double Final = (100.0 * MultiplicationResult1) / MultiplicationResult2;
cout<<"The percentage is "<<Final<<"%";
}
int main()
{
cout<<"This program will multiply fractions" <<endl;
MultiplyNumbers();
system("pause");
}
it works OK, if I understand?
Enter the First numerator: 2
Enter the First denominator: 3
Enter the Second numerator: 4
Enter the Second denominator: 5
2 x 4 = 8
--
3 x 5 = 15
The percentage is 53.3333%This program will multiply fractions
Press any key to continue . . .