Program to calculate Profit Or Loss
Given the Cost Price(CP) and Selling Price(SP) of a product. The task is to Calculate the Profit or Loss.
Examples:
Input: CP = 1500, SP = 2000
Output: 500 Profit
Input: CP = 3125, SP = 1125
Output: 2000 Loss
Formula:
Profit = (Selling Price - Cost Price)
Loss = (Cost Price - Selling Price)
Below is the required implementation:
C++
#include <iostream>
using namespace std;
int Profit( int costPrice, int sellingPrice)
{
int profit = (sellingPrice - costPrice);
return profit;
}
int Loss( int costPrice, int sellingPrice)
{
int Loss = (costPrice - sellingPrice);
return Loss;
}
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
class GFG
{
static int Profit( int costPrice,
int sellingPrice)
{
int profit = (sellingPrice - costPrice);
return profit;
}
static int Loss( int costPrice,
int sellingPrice)
{
int Loss = (costPrice - sellingPrice);
return Loss;
}
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 " );
}
}
|
Python 3
def Profit(costPrice, sellingPrice) :
profit = (sellingPrice - costPrice)
return profit
def Loss(costPrice, sellingPrice) :
Loss = (costPrice - sellingPrice)
return Loss
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" )
|
C#
using System;
class GFG
{
static int Profit( int costPrice,
int sellingPrice)
{
int profit = (sellingPrice - costPrice);
return profit;
}
static int Loss( int costPrice,
int sellingPrice)
{
int Loss = (costPrice - sellingPrice);
return Loss;
}
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 " );
}
}
|
PHP
<?php
function Profit( $costPrice ,
$sellingPrice )
{
$profit = ( $sellingPrice -
$costPrice );
return $profit ;
}
function Loss( $costPrice ,
$sellingPrice )
{
$Loss = ( $costPrice -
$sellingPrice );
return $Loss ;
}
$costPrice = 1500;
$sellingPrice = 2000;
if ( $sellingPrice == $costPrice )
echo "No profit nor Loss" ;
else if ( $sellingPrice > $costPrice )
echo Profit( $costPrice ,
$sellingPrice ). " Profit " ;
else
echo Loss( $costPrice ,
$sellingPrice ). " Loss " ;
?>
|
Javascript
<script>
function Profit(costPrice, sellingPrice)
{
let profit = (sellingPrice - costPrice);
return profit;
}
function Loss(costPrice, ellingPrice)
{
let Loss = (costPrice - sellingPrice);
return Loss;
}
let costPrice = 1500, sellingPrice = 2000;
if (sellingPrice == costPrice)
document.write( "No profit nor Loss" );
else if (sellingPrice > costPrice)
document.write(Profit(costPrice, sellingPrice) + " Profit " );
else
document.write(Loss(costPrice, sellingPrice) + " Loss " );
</script>
|
OUTPUT :
500 Profit
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
07 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...