Basic Programming C# Examples

C# Program to calculate Profit & Loss on transaction

The Program in C# Program to calculate Profit & Loss on transaction is given below:

using System;

namespace ProfitLossCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family,enter Cost price and selling price below  to check if its profit or loss: ");
            Console.WriteLine("Enter cost price: ");
            double costPrice = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Enter selling price: ");
            double sellingPrice = Convert.ToDouble(Console.ReadLine());

            double profit = sellingPrice - costPrice;
            if (profit > 0)
            {
                Console.WriteLine("Okay, the profit is : $" + profit);
            }
            else if (profit < 0)
            {
                Console.WriteLine("Loss: $" + Math.Abs(profit));
            }
            else
            {
                Console.WriteLine("Neither Profit nor Loss");
            }

            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family,enter Cost price and selling price below to check if its profit or loss: Enter cost price: 900
Enter selling price:
1000
Okay, the profit is : $100

Pro-Tips💡

Here are the step by step execution of above program:

  1. The code is compiled and executed in a console application.
  2. The program starts by displaying a message to the user: “Hello Codeauri Family,enter Cost price and selling price below to check if its profit or loss: “.
  3. The next message prompts the user to enter the cost price of the product: “Enter cost price: “.
  4. The user inputs the cost price, which is read by the program and stored in the variable “costPrice”.
  5. The next message prompts the user to enter the selling price of the product: “Enter selling price: “.
  6. The user inputs the selling price, which is read by the program and stored in the variable “sellingPrice”.
  7. The program calculates the profit by subtracting the cost price from the selling price and storing the result in the variable “profit”.
  8. The program checks if the profit is positive, negative, or zero using an “if” statement.
  9. If the profit is positive, a message is displayed: “Okay, the profit is : $” + profit.
  10. If the profit is negative, a message is displayed: “Loss: $” + Math.Abs(profit).
  11. If the profit is zero, a message is displayed: “Neither Profit nor Loss”.
  12. The program waits for the user to press Enter using Console.ReadLine().
  13. The program ends after the user presses Enter.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X