给定产品的成本价(CP)和销售价(SP)。任务是计算损益。
例子:
Input: CP = 1500, SP = 2000
Output: 500 Profit
Input: CP = 3125, SP = 1125
Output: 2000 Loss
公式:
Profit = (Selling Price - Cost Price)
Loss = (Cost Price - Selling Price)
以下是所需的实现:
C++
// C++ code to demonstrate Profit and Loss
#include
using namespace std;
// Function to calculate Profit.
int Profit(int costPrice, int sellingPrice)
{
int profit = (sellingPrice - costPrice);
return profit;
}
// Function to calculate Loss.
int Loss(int costPrice, int sellingPrice)
{
int Loss = (costPrice - sellingPrice);
return Loss;
}
// Driver Code.
int main()
{
int costPrice = 1500, sellingPrice = 2000;
if (sellingPrice == costPrice)
cout << "No profit nor Loss";
else if (sellingPrice > costPrice)
cout << Profit(costPrice, sellingPrice) << " Profit ";
else
cout << Loss(costPrice, sellingPrice) << " Loss ";
return 0;
}
Java
// Java code to demonstrate
// Profit and Loss
class GFG
{
// Function to calculate Profit.
static int Profit(int costPrice,
int sellingPrice)
{
int profit = (sellingPrice - costPrice);
return profit;
}
// Function to calculate Loss.
static int Loss(int costPrice,
int sellingPrice)
{
int Loss = (costPrice - sellingPrice);
return Loss;
}
// Driver Code.
public static void main(String[] args)
{
int costPrice = 1500,
sellingPrice = 2000;
if (sellingPrice == costPrice)
System.out.println("No profit nor Loss");
else if (sellingPrice > costPrice)
System.out.println(Profit(costPrice,
sellingPrice) +
" Profit ");
else
System.out.println(Loss(costPrice,
sellingPrice) +
" Loss ");
}
}
// This code is contributed
// by ChitraNayal
Python 3
# Python 3 program to demonstrate
# Profit and Loss
# Function to calculate Profit.
def Profit(costPrice, sellingPrice) :
profit = (sellingPrice - costPrice)
return profit
# Function to calculate Loss.
def Loss(costPrice, sellingPrice) :
Loss = (costPrice - sellingPrice)
return Loss
# Driver code
if __name__ == "__main__" :
costPrice, sellingPrice = 1500, 2000
if sellingPrice == costPrice :
print("No profit nor Loss")
elif sellingPrice > costPrice :
print(Profit(costPrice,
sellingPrice), "Profit")
else :
print(Loss(costPrice,
sellingPrice), "Loss")
# This code is contributed by ANKITRAI1
C#
// C# code to demonstrate Profit and Loss
using System;
class GFG
{
// Function to calculate Profit
static int Profit(int costPrice,
int sellingPrice)
{
int profit = (sellingPrice - costPrice);
return profit;
}
// Function to calculate Loss
static int Loss(int costPrice,
int sellingPrice)
{
int Loss = (costPrice - sellingPrice);
return Loss;
}
// Driver Code
public static void Main()
{
int costPrice = 1500,
sellingPrice = 2000;
if (sellingPrice == costPrice)
Console.Write("No profit nor Loss");
else if (sellingPrice > costPrice)
Console.Write(Profit(costPrice,
sellingPrice) + " Profit ");
else
Console.Write(Loss(costPrice,
sellingPrice) + " Loss ");
}
}
// This code is contributed by ChitraNayal
PHP
$costPrice)
echo Profit($costPrice,
$sellingPrice)." Profit ";
else
echo Loss($costPrice,
$sellingPrice)." Loss ";
// This code is contributed
// by ChitraNayal
?>
Javascript
输出 :
500 Profit