Program 08: ATM account balance swap using pointers.
Problem Statement:: In an ATM system, two account balances need to be swapped temporarily for validation. Develop a C program that accepts two balances and uses a function with pointers to swap them.
Problem Description:
Input: Two floating-point numbers representing account balances.
Output: The balances displayed both before and after the swap operation.
Constraints: Use pointers in the swap function to modify the original variables in the main memory.
Method: Implement a swap() function that accepts memory addresses (pointers).
Pgm Logic:
Start.
Input two balances: balance1 and balance2.
Display the balances before swapping.
Call the swap function, passing the addresses of the balances (&balance1, &balance2).
Inside the swap function:
Use a temporary variable to hold the value at the first pointer.
Assign the value at the second pointer to the first.
Assign the temporary value to the second pointer.
Display the updated balances in the main program.
Stop.
Program Code:
// Purpose: To swap two account balances in an ATM simulation using a function with pointers.
#include <stdio.h>
void swap(float *a, float *b)
{
float temp = *a;
*a = *b;
*b = temp;
}
void main()
{
float b1, b2;
printf("Enter balance for Account 1: ");
scanf("%f", &b1);
printf("Enter balance for Account 2: ");
scanf("%f", &b2);
printf("\nBefore Swapping:\nAccount 1: %.2f, Account 2: %.2f\n", b1, b2);
swap(&b1, &b2);
printf("\nAfter Swapping:\nAccount 1: %.2f, Account 2: %.2f\n", b1, b2);
}
Output:
Enter balance for Account 1: 1000.50
Enter balance for Account 2: 2000.75
Before Swapping: Account 1: 1000.50, Account 2: 2000.75
After Swapping: Account 1: 2000.75, Account 2: 1000.50
RESULT: Thus the program has been executed and the output was verified.
Remarks: This program was compiled and run in onlineGDB. It illustrates the concept of "Call by Reference," which is essential for functions that need to update original variables.
Program Explanation: In standard C functions, variables are usually copied (Call by Value). By using pointers (*a and *b) and passing addresses (&b1), the function gains direct access to the variables in the main memory, allowing it to swap their actual values.
For all 2026 published C Lab Program posts Index page: click here:
For all 2026 published articles list:click here:
…till the next post, bye-bye & take care