java program to input the Selling Price and Cost Price of a commodity and find the Profit or Loss made upon selling the product

Programming Example

Write a program to input the Selling Price and Cost Price of a commodity and find the Profit or Loss made upon selling the product.

For example,

INPUT:

Enter the Selling Price of the commodity: 456

Enter the Cost Price of the commodity: 400

OUTPUT:

Profit: 56

INPUT:

Enter the Selling Price of the commodity: 300

Enter the Cost Price of the commodity: 310

OUTPUT:

Loss: 10

Solution

import java.util.*;
class ProfitLoss
{
	public static void main(String arr[])
	{
		int sp,cp,ans;
		Scanner sc=new Scanner(System.in);
		System.out.println(“Enter the Selling Price of the commodity:”);
		sp=sc.nextInt();
		System.out.println(“Enter the Cost Price of the commodity:”);
		cp=sc.nextInt();
		if(sp>cp)
		{
			ans=sp-cp;
			System.out.println(“Profit:”+ans);
		}
		else if(sp<cp)
		{
			ans=cp-sp;
			System.out.println(“Loss:”+ans);
		}
		else
			System.out.println(“No profit or loss”);
	}
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad