Given the initial balance as bal and the amount X to be debited, where X must be a multiple of 10 and rupees 1.50 is deducted as the debit charge for each successful debit. The task is to find the remaining balance left after the transaction, which can be successful, or unsuccessful. The balances are in 2 floating-point precision.
Examples:
Input: X = 50, bal = 100.50 Output: 49.00 Transaction successful Input: X = 55, bal = 99.00 Output: 99.00 Transaction unsuccessful
Approach: Find out if the transaction can be successful or not.
- The transaction can be successful if:
- X is a multiple of 10, and
- The person has at least (X+1.50) rupees, that is the money to be withdrawn plus the charges, in the account.
- In any other case, the transaction will be unsuccessful.
- If the transaction is successful, then deduct the (X + 1.50) amount from the balance and return it
- Else just return the balance.
Below is the implementation of the above approach:
C++
// C++ program to find the remaining balance #include <bits/stdc++.h> 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 |
Javascript
<script> // JavaScript program to find the remaining balance // Function to find the balance function findBalance(x, bal) { // Check if the transaction // can be successful or not if (x % 10 == 0 && (x + 1.50) <= bal) { // Transaction is successful document.write( (bal - x - 1.50).toFixed(2)); } else { // Transaction is unsuccessful document.write( (bal).toFixed(2)); } } var x = 50; var bal = 100.50; findBalance(x, bal); </script> |
49.00
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!