Monday, December 8, 2025

Java program to calculate Profit or Loss based on Cost Price (CP) and Selling Price (SP).

 

Here is a simple and clean Java program to calculate Profit or Loss based on Cost Price (CP) and Selling Price (SP).


You can run it in any Java compiler (online/offline).

Java program to calculate Profit or Loss based on Cost Price (CP) and Selling Price (SP).


Java Program for Profit and Loss

import java.util.Scanner;

public class ProfitLossCalculator {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        // Input cost price and selling price
        System.out.print("Enter Cost Price (CP): ");
        double cp = sc.nextDouble();

        System.out.print("Enter Selling Price (SP): ");
        double sp = sc.nextDouble();

        // Calculate profit or loss
        if (sp > cp) {
            double profit = sp - cp;
            double profitPercent = (profit / cp) * 100;

            System.out.println("Profit: " + profit);
            System.out.println("Profit Percentage: " + profitPercent + "%");

        } else if (cp > sp) {
            double loss = cp - sp;
            double lossPercent = (loss / cp) * 100;

            System.out.println("Loss: " + loss);
            System.out.println("Loss Percentage: " + lossPercent + "%");

        } else {
            System.out.println("No Profit, No Loss.");
        }

        sc.close();
    }
}

How This Works

  • If Selling Price > Cost Price → Profit
  • If Cost Price > Selling Price → Loss
  • If both are equal → No Profit, No Loss

Percentages are calculated by:

Profit % = (Profit / CP) × 100
Loss % = (Loss / CP) × 100

⭐ Sample Output

Enter Cost Price (CP): 500
Enter Selling Price (SP): 650
Profit: 150.0
Profit Percentage: 30.0%


The Roadmap to Becoming an LLM Engineer in 2026

  The Roadmap to Becoming an LLM Engineer in 2026 Introduction The rise of Large Language Models (LLMs) has transformed the technology ind...