Back to: C#.NET Programs and Algorithms
Profit and Loss Program in C# with Examples
In this article, I am going to discuss how to Calculate Profit and Loss in C# with Examples. Please read our previous article, where we discussed Fahrenheit to Celsius in C#.
Profit and Loss in C#
- Cost Price (C.P.): The price at which the product is purchased, is called its cost price,
- Selling Price (S.P.): The price at which the product is sold, is called its selling price,
- Profit: If the Selling Price is greater than the Cost Price, the seller is said to have a profit or gain.
- Loss: If the Selling Price is less than the Cost Price, the seller is said to have incurred a loss.
Formula.
- Gain =(S.P.) – (C.P.)
- Loss = (C.P.) – (S.P.)
- Gain % = (Gain× 100÷C.P)
- Loss % = (Loss× 100÷C.P) \
- S.P = (100+Gain%× C.P ÷ 100)
- S.P = (100-Loss% × C.P ÷ 100)
- C.P = (100 × S.P ÷ 100 + Gain%)
- C.P= (100 × S.P ÷ 100 – Loss%).
- If the product is sold at a gain of say, 35%, then S.P. 135% of C.P.
- If the product is sold at a loss of say, 35%, then S.P. = 65% of C.P.
- When a person sells two similar items, one at a gain of say, x%, and the other At a loss of x%, then the seller always incurs a loss given by: Loss% = (Common Loss and Gain% ÷ 10) ^2 = (x÷10) ^2.
Where C.P is cost price, S.P is selling price, P is profit, and L is loss
Example:
A man buys one pen for Rs. 27.50 and sells it for Rs. 28.60. Find his gain.
Solution
C.P. = Rs. 27.50,
S.P. = Rs. 28.60.
So, profit = Rs. (28.60 – 27.50) = Rs. 1.10.
Logic to Calculate Profit and Loss in C#
To find the Profit and Loss simply we need to follow the below steps.
- First, we will create the functions for Profit and Loss.
- Then we will assign the value of cost price and selling price.
- After that we have to follow the following steps: Check if cost price > selling price, then it is a loss find the difference and return the result. Check if selling price > cost price, then it is a profit find the difference and return the result. And if selling price == cost price, then there is no profit nor a loss.
- After all the steps our result will print as output.
Example: Profit and Loss Program in C#.
The following C# Program will calculate the Profit and Loss.
using System; class ProfitAndLoss { // Function to calculate Profit static double Profit(double costPrice, double sellingPrice) { double profit = (sellingPrice - costPrice); return profit; } // Function to calculate Loss static double Loss(double costPrice, double sellingPrice) { double Loss = (costPrice - sellingPrice); return Loss; } public static void Main() { double costPrice = 1600, sellingPrice = 2000; Console.WriteLine(" Cost Price :" + costPrice); Console.WriteLine(" Selling Price :" + sellingPrice); if (sellingPrice == costPrice) Console.Write("No profit nor Loss"); else if (sellingPrice > costPrice) { Console.WriteLine(" Profit :" + Profit(costPrice, sellingPrice)); } else { Console.WriteLine(" Loss :" + Loss(costPrice, sellingPrice)); } Console.ReadKey(); } }
Output:
Example: Taking input from the user
In the following C# program, we will take the Cost Price and Selling Price from the user and then calculate the loss and profit and print it on the console.
using System; class ProfitAndLoss { // Function to calculate Profit static double Profit(double costPrice, double sellingPrice) { double profit = (sellingPrice - costPrice); return profit; } // Function to calculate Loss static double Loss(double costPrice, double sellingPrice) { double Loss = (costPrice - sellingPrice); return Loss; } public static void Main() { Console.WriteLine("Enter Cost Price "); double costPrice = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter Selling Price "); double sellingPrice = Convert.ToDouble(Console.ReadLine()); if (sellingPrice == costPrice) Console.Write("No profit nor Loss"); else if (sellingPrice > costPrice) { Console.WriteLine("Profit :" + Profit(costPrice, sellingPrice)); } else { Console.WriteLine("Loss :" + Loss(costPrice, sellingPrice)); } Console.ReadKey(); } }
Output
In the next article, I am going to discuss How to Convert Inches to Centimeter in C# with Examples. Here, in this article, I try to explain how to Calculate Profit and Loss in C# with Examples and I hope you enjoy this article.