How to show a percentile.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

void MultiplyNumbers ()
	double main()
{
	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 = (MultiplicationResult1 / MultiplicationResult2) * 100;
	cout<<"The percentage is "<<Final<<"%";

	cout<<"This program will multiply fractions" <<endl;

	MultiplyNumbers();
	system("pause");
}
In C++, an int divided by an int gives an int. What's 4/3? One. What's 8/10? Zero.

If you want an answer that isn't an int, change one of them to a double or float.
try

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
Last edited on
Yes but I only need to use float for the division not for the multiplication because it will always give me a whole number never a decimal.
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;
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int Compare(const void *elemA, const void *elemB);

int main ()
{
    int MultiplicationResult1 = 34;
    int MultiplicationResult2 = 123;

    double Double = (static_cast<double>(MultiplicationResult1) / MultiplicationResult2) * 100;
    double DivMul = (MultiplicationResult1 / MultiplicationResult2) / 100;
    double MulDiv = (100 * MultiplicationResult1) / MultiplicationResult2;

    printf("Double = %f\n", Double);
    printf("DivMul = %f\n", DivMul);
    printf("MulDiv = %f\n", MulDiv);

    return 0;
}


Double = 27.642276
DivMul = 0.000000
MulDiv = 27.000000




Last edited on
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.
Last edited on
First, I just noticed the start of your code

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

void MultiplyNumbers ()
	double main()
{
    ...
{


This doesn't look right.

I assume it's a mis-pasted version of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace 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>
using namespace std;

void MultiplyNumbers ()
{
    // do work here
}

int main() // MUST return int
{
    ...

    MultiplyNumbers ();

    return 0;
}


Modifying your program the second way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace 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 . . .

Last edited on
With int main()it now refuses to even run and I get an error message.
Well, the C++ (or C) main function is always expected to return an int (unless you're using a strange compiler).

What error message?
Last edited on
Wow thanks that worked exactly how I wanted it to thanks so much!!!
Topic archived. No new replies allowed.