C++ Program to Calculate the Percentage of a Number

Percentage

C++ programs to calculate the percentage of a number have been given below. A percentage is represented as a number or a ratio which is a fraction of 100. It is denoted by the symbol '%'. For example 25% represents (25 / 100) or 0.25. So, 25% of a number b will be calculated as b * (25 / 100) or b * 0.25.


Suppose a and b are two numbers. Then, the solution to the percentage problem is obtained by answering any of the following queries.

(i) What is a% of b?

(ii) a is what % of b?

(iii) a is b% of what?


Let's assume a = 15 and b = 60, then the solutions w.r.t the above queries:

(i) ___ is a% of b := 15% of 60 := 60 * (15 / 100) := 9

(ii) a is ___ % of b := (15 / 60) * 100 := 25%

(iii) a is b% of ___ := (15 / 60) * 100 := 25


In the following section, C++ programs to solve the above problems are given along with the algorithm, pseudocode and time-complexity of the programs.





1. Algorithm to calculate percentage of a number


1. Take a number a as input.

2. Take another number b as input where b% of a is to be calculated.

3. Calculate a * (b / 100) and declare it as the output.




2. Pseudocode to calculate percentage of a number


Input : Two numbers a, b

Output : b% of a

1. Procedure percentage(a, b):

2. x := a * (b / 100)

3. Return x

4. End Procedure





3. Time complexity to calculate percentage of a number


Time Complexity: O(1)




4. Programs to calculate percentage




4.1. C++ Program to calculate "What is a% of b?"

Code has been copied
/****************************************
	   alphabetacoder.com
C++ program to calculate what is a% of b?
*****************************************/

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    // declare variables
    float a, b, x;

    // take input
    cout << "Enter values to calculate a% of b" << endl;
    cout << "Enter a = ";
    cin >> a;
    cout << "Enter b = ";
    cin >> b;

    // calculate value
    x = b * (a / 100);

    // display result upto 3 decimal places
    cout << fixed << setprecision(3) << x << " is " << a << "% of " << b << endl;

    return 0;
}

Output


Case 1:

Enter values to calculate a% of b

Enter a = 65

Enter b = 150

97.500 is 65.000% of 150.000


Case 2:

Enter values to calculate a% of b

Enter a = 200

Enter b = 20

40.000 is 200.000% of 20.000





4.2. C++ Program to calculate "a is what % of b?"

Code has been copied
/*****************************************
	     alphabetacoder.com
C++ program to calculate a is what % of b?
******************************************/

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    // declare variables
    float a, b, x;

    // take input
    cout << "Enter values to calculate a is what % of b?" << endl;
    cout << "Enter a = ";
    cin >> a;
    cout << "Enter b = ";
    cin >> b;

    // calculate value
    x = (a / b) * 100;

    // display result upto 3 decimal places
    cout << fixed << setprecision(3) << a << " is " << x << "% of " << b << endl;

    return 0;
}

Output


Case 1:

Enter values to calculate a is what % of b?

Enter a = 10

Enter b = 30

10.000000 is 33.333% of 30.000000


Case 2:

Enter values to calculate a is what % of b?

Enter a = 30

Enter b = 45

30.000000 is 66.667% of 45.000000





4.3. C++ Program to calculate "a is b% of what?"

Code has been copied
/*****************************************
            alphabetacoder.com
C++ program to calculate a is b% of what?
******************************************/

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    // declare variables
    float a, b, x;

    // take input
    cout << "Enter values to calculate a is b % of what?" << endl;
    cout << "Enter a = ";
    cin >> a;
    cout << "Enter b = ";
    cin >> b;

    // calculate value
    x = (a / b) * 100;

    // display result upto 3 decimal places
    cout << fixed << setprecision(3) << a << " is " << b << "% of " << x << endl;

    return 0;
}

Output


Case 1:

Enter values to calculate a is b % of what?

Enter a = 13

Enter b = 35

13.000 is 35.000% of 37.143


Case 2:

Enter values to calculate a is b % of what?

Enter a = 16.66

Enter b = 75

16.660 is 75.000% of 22.213