برنامه محاسبه سود و زیان — راهنمای کاربردی

آخرین به‌روزرسانی: ۶ بهمن ۱۳۹۸
زمان مطالعه: ۳ دقیقه
برنامه محاسبه سود و زیان -- راهنمای کاربردی

در این مطلب، روش نوشتن برنامه محاسبه سود و زیان بیان و پیاده‌سازی آن در زبان‌های برنامه‌نویسی گوناگون شامل «سی‌پلاس‌پلاس» (++C)، «جاوا» (Java)، «پایتون» (Python)، «سی‌شارپ» (#C) و «پی‌اچ پی» (PHP) انجام شده است.

برنامه محاسبه سود و زیان

«قیمت خرید» (Cost Price | CP) و «قیمت فروش» (Selling Price | SP) یک محصول داده شده است. هدف، محاسبه سود یا زیان است. مثال زیر به درک بهتر این مسئله، کمک می‌کند.

Input: CP = 1500, SP = 2000
Output: 500 Profit

Input: CP = 3125, SP = 1125
Output: 2000 Loss

برای محاسبه سود و زیان، از رابطه‌های زیر استفاده می‌شود.

  • قیمت خرید – قیمت فروش = سود
  • قیمت فروش – قیمت خرید = زیان

در ادامه، پیاده‌سازی آنچه بیان شد، در زبان‌های برنامه‌نویسی گوناگون انجام شده است. شایان توجه است که در برنامه‌های ارائه شده، فرمول سود پیاده‌سازی شده است. برای محاسبه زیان، می‌توان کد را تغییر داد و فرمول محاسبه زیان را که در بالا نیز به آن اشاره شده است، جایگزین کرد.

برنامه محاسبه سود و زیان در ++C

// C++ code to demonstrate Profit and Loss 
#include <iostream> 
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 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 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

<?php  
// PHP code to demonstrate  
// Profit and Loss 
  
// Function to calculate Profit. 
function Profit($costPrice,  
                $sellingPrice) 
{ 
    $profit = ($sellingPrice - 
               $costPrice); 
  
    return $profit; 
} 
  
// Function to calculate Loss. 
function Loss($costPrice, 
              $sellingPrice) 
{ 
    $Loss = ($costPrice -  
             $sellingPrice); 
  
    return $Loss; 
} 
  
// Driver Code. 
$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 "; 
  
// This code is contributed 
// by ChitraNayal 
?>

خروجی قطعه کد بالا، به صورت زیر است.

500 Profit

اگر نوشته بالا برای شما مفید بوده است، آموزش‌های زیر نیز به شما پیشنهاد می‌شوند:

^^

بر اساس رای ۲ نفر
آیا این مطلب برای شما مفید بود؟
شما قبلا رای داده‌اید!
اگر بازخوردی درباره این مطلب دارید یا پرسشی دارید که بدون پاسخ مانده است، آن را از طریق بخش نظرات مطرح کنید.
منابع:
GeeksforGeeks

نظر شما چیست؟

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *