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 $\frac{25}{100}$ or 0.25. So, 25% of a number $b$ will be calculated as $(b \times \frac{25}{100})$ or $(b \times 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 $\Rightarrow 15 \%$ of $60 = 60 \times \frac{15}{100} = 9$
(ii) a is ___ % of b $\Rightarrow \frac{15}{60} \times 100 = 25\%$
(iii) a is b% of ___ $\Rightarrow \frac{15}{60} \times 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.
Page content(s):
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.
3.
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?"
/*************************************** alphabetacoder.com C program to calculate what is a% of b? ****************************************/ #include <stdio.h> int main() { // declare variables float a, b, x; // take input printf("Enter values to calculate a%% of b\n"); printf("Enter a = "); scanf("%f", & a); printf("Enter b = "); scanf("%f", & b); // calculate value x = b * (a / 100); // display result upto 3 decimal places printf("%0.3f is %f%% of %f", x, a, b); return 0; }
Output
Case 1:
Enter values to calculate a% of b
Enter a = 20
Enter b = 60
12.000 is 20.000000% of 60.000000
Case 2:
Enter values to calculate a% of b
Enter a = 12.25
Enter b = 18
2.205 is 12.250000% of 18.000000
4.2. C Program to calculate "a is what % of b?"
/*************************************** alphabetacoder.com C program to calculate a is what % of b? ****************************************/ #include <stdio.h> int main() { // declare variables float a, b, x; // take input printf("Enter values to calculate a is what %% of b?\n"); printf("Enter a = "); scanf("%f", & a); printf("Enter b = "); scanf("%f", & b); // calculate value x = (a / b) * 100; // display result upto 3 decimal places printf("%f is %0.3f%% of %f", a, x, b); 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?"
/*************************************** alphabetacoder.com C program to calculate a is b% of what? ****************************************/ #include <stdio.h> int main() { // declare variables float a, b, x; // take input printf("Enter values to calculate a is b %% of what?\n"); printf("Enter a = "); scanf("%f", & a); printf("Enter b = "); scanf("%f", & b); // calculate value x = (a / b) * 100; // display result upto 3 decimal places printf("%f is %f%% of %0.3f", a, b, x); return 0; }
Output
Case 1:
Enter values to calculate a is b % of what?
Enter a = 20
Enter b = 40
20.000000 is 40.000000% of 50.000
Case 2:
Enter values to calculate a is b % of what?
Enter a = 75
Enter b = 75
75.000000 is 75.000000% of 100.000