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 40% represents $\frac{40}{100}$ or 0.40. So, 40% of a number $b$ will be calculated as $(b \times \frac{40}{100})$ or $(b \times 0.40)$.


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.





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 \leftarrow a \times \frac{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?
****************************************/

using System;
namespace Percentage {
    class Program {
        static void Main(string[] args) {
            // declare variables
            float a, b, x;

            // take input
            Console.WriteLine("Enter values to calculate a% of b");
            Console.Write("Enter a = ");
            a = float.Parse(Console.ReadLine());
            Console.Write("Enter b = ");
            b = float.Parse(Console.ReadLine());

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

            // display result upto 3 decimal places
            Console.WriteLine("{0:F3} is {1:F3}% of {2:F3}", x, a, b);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter values to calculate a% of b

Enter a = 20

Enter b = 60

12.000 is 20.000% of 60.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?
****************************************/

using System;
namespace Percentage {
    class Program {
        static void Main(string[] args) {
            // declare variables
            float a, b, x;

            // take input
            Console.WriteLine("Enter values to calculate a is what% of b?");
            Console.Write("Enter a = ");
            a = float.Parse(Console.ReadLine());
            Console.Write("Enter b = ");
            b = float.Parse(Console.ReadLine());

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

            // display result upto 3 decimal places
            Console.WriteLine("{0:F3} is {1:F3}% of {2:F3}", a, x, b);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter values to calculate a is what% of b?

Enter a = 30

Enter b = 45

30.000 is 66.667% of 45.000





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?
****************************************/

using System;
namespace Percentage {
    class Program {
        static void Main(string[] args) {
            // declare variables
            float a, b, x;

            // take input
            Console.WriteLine("Enter values to calculate a is b% of what?");
            Console.Write("Enter a = ");
            a = float.Parse(Console.ReadLine());
            Console.Write("Enter b = ");
            b = float.Parse(Console.ReadLine());

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

            // display result upto 3 decimal places
            Console.WriteLine("{0:F3} is {1:F3}% of {2:F3}", a, b, x);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter values to calculate a is b% of what?

Enter a = 20

Enter b = 40

20.000 is 40.000% of 50.000