Contact Learn C
Copy Program Share Program

Program 278: Program for ATM Machine

Program 278: Program for ATM Machine

#include<stdio.h>
main()
{
 int i,choice;
 float cash=0;
 char c;
 
 do{
  printf("Enter\n1-Withdraw\n2-Deposit\n3-Check Balance\n");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
  {
   int withdraw;
   printf("Enter Amount to withdraw\n");
   scanf("%d",&withdraw);
   if(withdraw%100==0)
   {
    if(cash>=withdraw)
        {
        cash-=withdraw;
        printf("Amount After withdrawl of cash is %f\n",cash);
       }
       else
    {
    printf("You don't have enough Amount to Withdraw.Please Deposit Amount\n");
        }
   }
   else
   {
    printf("Enter Withdrawl Amount in 100's\n");
   }
   break;
  }
  case 2:
  {
   int deposit;
   printf("Enter Amount to deposit\n");
   scanf("%d",&deposit);
   if(deposit%100==0)
   {
       cash=cash+deposit;
       printf("Balance After Depositing Amount is %f\n",cash);
   }
   else
   {
    printf("Please Enter Amount in 100's\n");
   }
   break;
  }
  case 3:
  {
   printf("Balance in the Account is %.2f\n",cash);
   break;
  }
  default :
  {
   printf("Enter Valid Choice\n");
   break;
  }
 }
 printf("To Continue Press 'Y' else any letter\n");
 fflush(stdin);
 scanf("%c",&c);
   }while(c=='y' || c=='Y');
   printf("Thanks for using our ATM\n");
}
Explanation:

  1. This program starts with initializing :
    • i,c → used as helping variable
    • choice→ To choose options
    • cashTo store final money by default is zero
  2. Now Lets us Choose Option 2 to deposit money of 2000 rupees
    int deposit;
       printf("Enter Amount to deposit\n");
       scanf("%d",&deposit);
       if(deposit%100==0)
       {
           cash=cash+deposit;
           printf("Balance After Depositing Amount is %f\n",cash);
       }
       else
       {
        printf("Please Enter Amount in 100's\n");
       }
    
    This is the logic for option 2.Now we need to add deposit amount and this is stored in variable "deposit".Now we are checking whether depositing amount is in multiple of 100's or not.Only if the amount that is deposited in  multiples of 100's to avoid 50' and 10's etc
    if(deposit%100==0)
    
    To check whether deposited amount is in 100;s we are checking it as given above.Then we are adding "deposit" to "cash".Now cash=2000 as we enetered deposit amount as 2000.
  3. If the deposit amount is not in 100's it will throw an error to Enter Amount in 100's


  4. Now lets choose option 1 to withdraw money of 2300 and now cash is having 2000 rupees
    if(withdraw%100==0)
       {
        if(cash>=withdraw)
            {
            cash-=withdraw;
            printf("Amount After withdrawl of cash is %f\n",cash);
           }
           else
        {
        printf("You don't have enough Amount to Withdraw.Please Deposit Amount\n");
            }
       }
       else
       {
        printf("Enter Withdrawl Amount in 100's\n");
       }
    
    First of all we are checking if withdrwl amount is multiples of 100 or not and as 2300 is multiple of 100 then we are checking if cash>=withdrawl amount .In this case 2000 is not greater than 2000 so it will throw an error saying "You don't have enough Amount to Withdraw.Please Deposit Amount"
  5. Now lets withdraw 1800 so withdraw=1800.Now again we are checking 1800 is multiples of 100 or not ans as it is multiple of 100 we are going to check whether 2000>1800 (cash>=withdraw) and as it is true we are reducing withdraw amount from cash. So Now cash=2000-1800=200
  6.  printf("Balance in the Account is %.2f\n",cash);
  7. Finally displaying balance which is option 3
  8. And all this will be in do while loop so that user can continue to do transactions like withdraw and deposit until he stops using 
    do{
    ...............
    ................
    .................
    .................
    .................
    .................
    printf("To Continue Press 'Y' else any letter\n");
     fflush(stdin);
     scanf("%c",&c);
       }while(c=='y' || c=='Y');
    
    so if user chooses other than 'y' he will go out of this loop else continues to do transactions 
  9. Finally after finishing his/her transactions we are greeting with
  10.    printf("Thanks for using our ATM\n");

Output:
Program for ATM Machine

Program for ATM Machine






Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide