Software Development Exam  >  Software Development Questions  >  Write a program in java to accept the cost pr... Start Learning for Free
Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and displa
y profit percent, else display loss percent?
Most Upvoted Answer
Write a program in java to accept the cost price and selling price of ...
Here’s a Java program that accepts the cost price and selling price of an article, determines whether there is a profit or loss, and calculates the respective percentages.
java
import java.util.Scanner;
public class ProfitLossCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept cost price
System.out.print("Enter the cost price: ");
double costPrice = scanner.nextDouble();
// Accept selling price
System.out.print("Enter the selling price: ");
double sellingPrice = scanner.nextDouble();
// Calculate profit or loss
double difference = sellingPrice - costPrice;
if (difference > 0) {
double profitPercent = (difference / costPrice) * 100;
System.out.printf("Profit: %.2f%%\n", profitPercent);
} else if (difference < 0)="" />
double lossPercent = (Math.abs(difference) / costPrice) * 100;
System.out.printf("Loss: %.2f%%\n", lossPercent);
} else {
System.out.println("No profit, no loss.");
}
scanner.close();
}
}
Program Explanation:
- Scanner Class: This program uses the `Scanner` class to capture user input for the cost price and selling price of the article.
- Variables:
- `costPrice`: Stores the purchase cost of the article.
- `sellingPrice`: Stores the price at which the article is sold.
- `difference`: Calculates the difference between selling price and cost price.
- Profit or Loss Calculation:
- If the difference is greater than zero, it indicates a profit. The profit percentage is calculated using the formula:
- Profit Percent = (Profit / Cost Price) * 100
- If the difference is less than zero, it indicates a loss. The loss percentage is calculated using the formula:
- Loss Percent = (Loss / Cost Price) * 100
- If the difference is zero, it indicates that there is neither profit nor loss.
This program effectively provides clarity on the financial outcome of the transaction, ensuring users can make informed decisions based on profit or loss percentages.
Explore Courses for Software Development exam

Top Courses for Software Development

Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent?
Question Description
Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? for Software Development 2025 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? covers all topics & solutions for Software Development 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent?.
Solutions for Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? defined & explained in the simplest way possible. Besides giving the explanation of Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent?, a detailed solution for Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? has been provided alongside types of Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? theory, EduRev gives you an ample number of questions to practice Write a program in java to accept the cost price and selling price of an article using the scanner class. Check whether it incurs a profit or loss. If profit happens,then find and display profit percent, else display loss percent? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev