给定和表示N篇文章的成本价格等于M篇文章的售价。任务是确定利润或亏损百分比。
例子:
Input: N = 8, M = 9
Output: Loss = -11.11%
Input: N = 8, M = 5
Output: Profit = 60%
公式:-
下面是上述方法的实现:
C++
// C++ implementation of above formula
#include
using namespace std;
// Function to calculate
// Profit or loss
void profitLoss(int N, int M)
{
if (N == M)
cout << "No Profit nor Loss";
else {
float result = 0.0;
result = float(abs(N - M)) / M;
if (N - M < 0)
cout << "Loss = -" << result * 100 << "%";
else
cout << "Profit = " << result * 100 << "%";
}
}
// Driver Code
int main()
{
int N = 8, M = 9;
profitLoss(N, M);
return 0;
}
Java
// Java implementation of above formula
public class GFG{
// Function to calculate
// Profit or loss
static void profitLoss(int N, int M)
{
if (N == M)
System.out.print("No Profit nor Loss");
else {
float result = 0;
result = (float)(Math.abs(N - M))/ M;
if (N - M < 0)
System.out.print("Loss = -" + result * 100 +"%");
else
System.out.print("Profit = " + result * 100 + "%");
}
}
// Driver Code
public static void main(String []args){
int N = 8, M = 9;
profitLoss(N, M);
}
// This code is contributed by ANKITRAI1
}
Python3
# Python 3 implementation of above formula
# Function to calculate Profit or loss
def profitLoss(N, M):
if (N == M):
print("No Profit nor Loss")
else:
result = 0.0
result = float(abs(N - M)) / M
if (N - M < 0):
print("Loss = -",'{0:.6}' .
format(result * 100), "%")
else:
print("Profit = ",'{0:.6}' .
format(result * 100), "%")
# Driver Code
if __name__ == '__main__':
N = 8
M = 9
profitLoss(N, M)
# This code is contributed by
# Sanjit_Prasad
C#
// C# implementation of above formula
using System;
class GFG
{
// Function to calculate
// Profit or loss
static void profitLoss(int N, int M)
{
if (N == M)
Console.Write("No Profit nor Loss");
else
{
float result = 0;
result = (float)(Math.Abs(N - M))/ M;
if (N - M < 0)
Console.Write("Loss = -" +
result * 100 +"%");
else
Console.Write("Profit = " +
result * 100 + "%");
}
}
// Driver Code
static public void Main ()
{
int N = 8, M = 9;
profitLoss(N, M);
}
}
// This code is contributed by ajit.
PHP
Javascript
输出:
Loss = -11.1111%
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。