Calculate Profit and Loss in C / C++ / Java / Python / C#

Profit and loss

Programs to calculate profit and loss have been shown here. The formula to calculate profit and loss is

   Profit and Loss = Selling Price - Cost Price

If the selling price is more than the cost price, we get the profit, and if the cost price is more than the selling price, we get the loss. If both are the same, it is considered no profit or loss.






1. Algorithm to calculate profit and loss


1. Take cost price cp and selling price sp as input.

2. Calculate the difference pl = sp - cp/

3. If the value of pl is positive, display pl as the profit.

4. If the value of pl is negative, display pl as the loss.

5. If the value of pl is zero, display "no profit, no loss"




2. Pseudocode to calculate profit and loss


Input : Cost price $cp$, Selling price $sp$

Output : Profit and loss

1. Procedure profitLoss($cp$, $sp$):

2. $pl = sp - cp$

3. If $pl > 0$:

4. Return $pl$ as profit

5. Else If $pl < 0$:

6. Return $|pl|$ as loss

7. Else:

8. Return "No profit, no loss"

9. End Procedure





3. Time complexity to calculate profit and loss


Time Complexity: O(1)





4. Flowchart to calculate profit and loss

Flowchart to calculate profit and loss



5. Program to calculate profit and loss

The following section covers the C, C++, Java, Python and C# programs to calculate profit and loss. The user needs to input the cost and selling prices to calculate the profit and loss.




5.1. C Program to calculate profit and loss

Code has been copied
/*************************************
 	alphabetacoder.com
C program to calculate profit and loss
**************************************/

#include <stdio.h>

int main() {
    // declare variables
    double cp, sp, pl;

    // take input 
    printf("Enter the cost price: ");
    scanf("%lf", & cp);
    printf("Enter the selling price: ");
    scanf("%lf", & sp);

    // calculate profit and loss
    pl = sp - cp;

    // display the result
    if (pl > 0)
        printf("Profit = %lf", pl);
    else if (pl < 0)
        printf("Loss = %lf", -(pl));
    else
        printf("No profit, no loss");

    return 0;
}

Output


Case 1:

Enter the cost price: 20.25

Enter the selling price: 25.75

Profit = 5.500000


Case 2:

Enter the cost price: 30

Enter the selling price: 22.5

Loss = 7.500000


Case 3:

Enter the cost price: 10.25

Enter the selling price: 10.25

No profit, no loss




5.2. C++ Program to calculate profit and loss

Code has been copied
/***************************************
 	   alphabetacoder.com
C++ program to calculate profit and loss
****************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    double cp, sp, pl;

    // take input 
    cout << "Enter the cost price: ";
    cin >> cp;
    cout << "Enter the selling price: ";
    cin >> sp;

    // calculate profit and loss
    pl = sp - cp;

    // display the result
    if (pl > 0)
        cout << "Profit = " << pl;
    else if (pl < 0)
        cout << "Loss = " << -(pl);
    else
        cout << "No profit, no loss";

    return 0;
}

Output


Case 1:

Enter the cost price: 36.5

Enter the selling price: 64.8

Profit = 28.3


Case 2:

Enter the cost price: 100

Enter the selling price: 50

Loss = 50


Case 3:

Enter the cost price: 76

Enter the selling price: 76

No profit, no loss




5.3. Java Program to calculate profit and loss

Code has been copied
/****************************************
	   alphabetacoder.com
Java program to calculate profit and loss
*****************************************/

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // declare variables
        double cp, sp, pl;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input
        System.out.print("Enter the cost price: ");
        cp = sc.nextDouble();
        System.out.print("Enter the selling price: ");
        sp = sc.nextDouble();

        // calculate profit and loss
        pl = sp - cp;

        // display the result
        if (pl > 0)
            System.out.print("Profit = " + pl);
        else if (pl < 0)
            System.out.print("Loss = " + -(pl));
        else
            System.out.print("No profit, no loss");
    }
}

Output


Case 1:

Enter the cost price: 134

Enter the selling price: 200

Profit = 66.0


Case 2:

Enter the cost price: 45

Enter the selling price: 39.25

Loss = 5.75


Case 3:

Enter the cost price: 18.5

Enter the selling price: 18.5

No profit, no loss




5.4. Python Program to calculate profit and loss

Code has been copied
# *******************************************
# 	    alphabetacoder.com
# Python program to calculate profit and loss
# *******************************************

# take input
cp = float(input("Enter the cost price: "))
sp = float(input("Enter the selling price: "))

# calculate profit and loss
pl = sp - cp

# display the result
if pl > 0:
    print("Profit = ", pl)
elif pl < 0:
    print("Loss = ", -(pl))
else:
    print("No profit, no loss")

Output


Case 1:

Enter the cost price: 500

Enter the selling price: 720

Profit = 220.0


Case 2:

Enter the cost price: 60

Enter the selling price: 35.5

Loss = 24.5


Case 3:

Enter the cost price: 88

Enter the selling price: 88

No profit, no loss




5.5. C# Program to calculate profit and loss

Code has been copied
/***************************************
	   alphabetacoder.com
C# program to calculate profit and loss
****************************************/

using System;

namespace ProfitLoss {
    class Program {
        static void Main(string[] args) {
            // declare variables
            double cp, sp, pl;

            // take input
            Console.Write("Enter the cost price: ");
            cp = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the selling price: ");
            sp = Convert.ToDouble(Console.ReadLine());

            // calculate profit and loss
            pl = sp - cp;

            // display the result
            if (pl > 0)
                Console.WriteLine("Profit = " + pl);
            else if (pl < 0)
                Console.WriteLine("Loss = " + -(pl));
            else
                Console.WriteLine("No profit, no loss");

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

Output


Case 1:

Enter the cost price: 350

Enter the selling price: 450

Profit = 100


Case 2:

Enter the cost price: 60

Enter the selling price: 40

Loss = 20


Case 3:

Enter the cost price: 100

Enter the selling price: 100

No profit, no loss