Cost Price Per Item Program in Java


If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one item.


The program prompts the user to enter the selling price and profit using the System.out.println statement and then reads the inputs using the Scanner class.

The program then calculates the cost price of one item using the formula: (selling price - profit) / 15. The result is stored in the variable cp.

Finally, the program outputs the cost price of one item using the System.out.println statement.

Source Code

import java.util.Scanner;
class CostPrice_OneItem
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the Selling Price : ");
		float sp  = input.nextInt();
		System.out.println("Enter the Profit  : ");
		float profit  = input.nextInt();
		float cp = sp - profit;
		cp = cp / 15;
		System.out.println("Cost Price of Per Item :"+cp);
	}
}

Output

Enter the Selling Price :
7850
Enter the Profit  :
590
Cost Price of Per Item :484.0