Profit or Loss
Determine profit or loss based on cost price and selling price.
BeginnerTopic: Module 2: Conditional Programs
Java Profit or Loss Program
This program helps you to learn the fundamental structure and syntax of Java programming.
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
Understanding Profit or Loss
We compare selling price to cost price and compute either profit or loss accordingly.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.