Computer Applications

Write a program to input the cost price and the selling price of an article. If the selling price is more than the cost price then calculate and display actual profit and profit per cent otherwise, calculate and display actual loss and loss per cent. If the cost price and the selling price are equal, the program displays the message 'Neither profit nor loss'.

Java

Java Conditional Stmts

205 Likes

Answer

import java.util.Scanner;

public class KboatProfit
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter cost price of the article: ");
        double cp = in.nextDouble();
        System.out.print("Enter selling price of the article: ");
        double sp = in.nextDouble();
        double pl = sp - cp;
        double percent = Math.abs(pl) / cp * 100;
        if (pl > 0) {
            System.out.println("Profit = " + pl);
            System.out.println("Profit % = " + percent);
        }
        else if (pl < 0) {
            System.out.println("Loss = " + Math.abs(pl));
            System.out.println("Loss % = " + percent);
        }
        else {
            System.out.println("Neither profit nor loss");
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

71 Likes


Related Questions