Profit or Loss

Determine profit or loss based on cost price and selling price.

JavaBeginner
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter cost price: ");
        double cp = sc.nextDouble();
        System.out.print("Enter selling price: ");
        double sp = sc.nextDouble();

        if (sp > cp) {
            System.out.println("Profit = " + (sp - cp));
        } else if (sp < cp) {
            System.out.println("Loss = " + (cp - sp));
        } else {
            System.out.println("No profit, no loss");
        }

        sc.close();
    }
}

Output

Enter cost price: 100
Enter selling price: 120
Profit = 20.0

We compare selling price to cost price and compute either profit or loss accordingly.