📜  找到交易后的余额

📅  最后修改于: 2021-05-07 05:42:25             🧑  作者: Mango

给定初始余额为bal和要借记的金额X ,其中X必须是10的倍数,并且每次成功借记都要扣除1.50卢比作为借记费用。任务是找到交易后剩余的余额,这可以是成功的,也可以是不成功的。天平的精度为2个浮点数。

例子:

Input: X = 50, bal = 100.50
Output: 49.00
Transaction successful

Input: X = 55, bal = 99.00
Output: 99.00
Transaction unsuccessful

方法:找出交易是否成功。

  • 在以下情况下,交易可以成功:
    • X是10的倍数,并且
    • 该人的帐户中至少有(X + 1.50)卢比,即要提取的钱加上费用。
  • 在任何其他情况下,交易均不会成功。
  • 如果交易成功,则从余额中扣除(X + 1.50)金额并退还
  • 否则就退还余额。

下面是上述方法的实现:

C++
// C++ program to find the remaining balance
  
#include 
using namespace std;
  
// Function to find the balance
void findBalance(int x, float bal)
{
  
    // Check if the transaction
    // can be successful or not
    if (x % 10 == 0
        && ((float)x + 1.50) <= bal) {
  
        // Transaction is successful
        cout << fixed << setprecision(2)
             << (bal - x - 1.50) << endl;
    }
    else {
  
        // Transaction is unsuccessful
        cout << fixed << setprecision(2)
             << (bal) << endl;
    }
}
  
int main()
{
  
    int x = 50;
    float bal = 100.50;
  
    findBalance(x, bal);
  
    return 0;
}


Java
// Java program to find the remaining balance
import java.util.*;
  
class GFG 
{
  
// Function to find the balance
static void findBalance(int x, float bal)
{
  
    // Check if the transaction
    // can be successful or not
    if (x % 10 == 0 && ((float)x + 1.50) <= bal) 
    {
  
        // Transaction is successful
        System.out.printf("%.2f\n", bal - x - 1.50);
    }
    else 
    {
  
        // Transaction is unsuccessful
        System.out.printf("%.2f\n", bal);
    }
}
  
// Driver Code
public static void main(String[] args) 
{
    int x = 50;
    float bal = (float) 100.50;
  
    findBalance(x, bal);
}
}
  
// This code is contributed by Princi Singh


Python3
# Python3 program to find the remaining balance
  
# Function to find the balance
def findBalance(x,bal):
  
    # Check if the transaction
    # can be successful or not
    if (x % 10 == 0 and (x + 1.50) <= bal):
  
        #Transaction is successful
        print(round(bal - x - 1.50, 2))
    else:
  
        # Transaction is unsuccessful
        print(round(bal, 2))
  
# Driver Code
x = 50
bal = 100.50
  
findBalance(x, bal)
  
# This code is contributed by Mohit Kumar


C#
// C# program to find the remaining balance
using System;
  
class GFG 
{
  
// Function to find the balance
static void findBalance(int x, float bal)
{
  
    // Check if the transaction
    // can be successful or not
    if (x % 10 == 0 && ((float)x + 1.50) <= bal) 
    {
  
        // Transaction is successful
        Console.Write("{0:F2}\n", bal - x - 1.50);
    }
    else
    {
  
        // Transaction is unsuccessful
        Console.Write("{0:F2}\n", bal);
    }
}
  
// Driver Code
public static void Main(String[] args) 
{
    int x = 50;
    float bal = (float) 100.50;
  
    findBalance(x, bal);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
49.00