In the previous article, we have seen Java Program to Calculate Generic Root of a Number
In this article we will see how we can calculate profit and loss by using Java programming language.
Java Program to Calculate Profit and Loss
Before jumping into the program let’s know how we calculate profit and loss. Profit And Loss Program In Java or Profit Loss Program In Java, Profit Or Loss Program In Java.
As we know every product has a value or cost price. When selling price crosses the cost price then we say it as profit and when the selling price is below the actual product’s cost price then we say it as loss.
Profit= Selling Price - Cost Price Loss= Cost Price - Selling Price
Let’s see different ways to calculate profit and loss.
Method-1: Java Program to Calculate Profit and Loss By Using Static Input Value
Approach:
- Declare the product’s actual price, selling price.
- If selling price is more than product’s actual price then
sellingPrice-actualPrice
will give profit. - If selling price is less than product’s actual price then
actualPrice-sellingPrice
will give loss.
Program:
import java.util.*; class Main { public static void main(String args[]) { //product's actual price double actualPrice=11000; //product's selling price double sellingPrice=12000; //checking if selling price is more than produt actual price //selling price is more than product's actual price which is profit if(sellingPrice>actualPrice) { System.out.println("Profit :" + (sellingPrice-actualPrice)); } //selling price is less than product's actual price which is profit else if(sellingPrice<actualPrice) { System.out.println("Loss :" + (actualPrice-sellingPrice)); } else System.out.println("No profit and no loss"); } }
Output: Profit :1000.0
Method-2: Java Program to Calculate Profit and Loss By Using User Input Value
Approach:
- Take user input of product’s actual price, selling price.
- If selling price is more than product’s actual price then
sellingPrice-actualPrice
will give profit. - If selling price is less than product’s actual price then
actualPrice-sellingPrice
will give loss.
Program:
import java.util.*; class Main { public static void main(String args[]) { //Scanner class object created Scanner s= new Scanner(System.in); //asking the user to take input of product's actual price System.out.println("Enter the product's actual price: "); double actualPrice=s.nextDouble(); //asking the user to take input of product's selling price System.out.println("Enter the product's selling price: "); double sellingPrice=s.nextDouble(); //checking if selling price is more than produt actual price //selling price is more than product's actual price which is profit if(sellingPrice>actualPrice) { System.out.println("Profit :" + (sellingPrice-actualPrice)); } //selling price is less than product's actual price which is profit else if(sellingPrice<actualPrice) { System.out.println("Loss :" + (actualPrice-sellingPrice)); } else System.out.println("No profit and no loss"); } }
Output: Case-1 Enter the product's actual price: 36000 Enter the product's selling price: 40000 Profit :4000.0 Case-2 Enter the product's actual price: 6700 Enter the product's selling price: 6500 Loss :200.0
Method-3: Java Program to Calculate Profit and Loss By Using User Defined Method
Approach:
- Take user input of product’s actual price, selling price.
- Then call
profitLoss()
method by passingsellingPrice and actualPrice
as parameter. - If selling price is more than product’s actual price then
sellingPrice-actualPrice
will give profit. - If selling price is less than product’s actual price then
actualPrice-sellingPrice
will give loss.
Program:
import java.util.*; class Main { public static void main(String args[]) { //Scanner class object created Scanner s= new Scanner(System.in); //asking the user to take input of product's actual price System.out.println("Enter the product's actual price: "); double actualPrice=s.nextDouble(); //asking the user to take input of product's selling price System.out.println("Enter the product's selling price: "); double sellingPrice=s.nextDouble(); //calling profitLoss() method to find profit or loss profitLoss(actualPrice,sellingPrice); } //profitLoss() method public static void profitLoss(double actualPrice, double sellingPrice) { //checking if selling price is more than produt actual price //selling price is more than product's actual price which is profit if(sellingPrice>actualPrice) { System.out.println("Profit :" + (sellingPrice-actualPrice)); } //selling price is less than product's actual price which is profit else if(sellingPrice<actualPrice) { System.out.println("Loss :" + (actualPrice-sellingPrice)); } else System.out.println("No profit and no loss"); } }
Output: Enter the product's actual price: 8800 Enter the product's selling price: 10000 Profit :1200.0
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Applications On:
- Write A Program To Calculate Profit Or Loss In Java
- Profit Development Code In Java
- To Calculate Profit-Loss For Given Cost And Sell Price In Python
- Write A Program To Calculate Profit Or Loss
- Write A Python Program To Calculate Profit Or Loss
Related Java Programs: