Switch statement is known as multiway decision statement. It can be used at the palace of If statement. Some times we more number of conditions the we get puzzled that which condition is used for which variable. So we use Switch statement.
In switch we use case for matching the given variable. The switch statement test the value of given variable in a list of case values and when a match is found a block of statements associated with that case is executed.
ATM program on Switch statement-
Program perform the ATM operation Password? then check
1. Mini statement
2. Withdraw Amount
3. Deposit Amount.
Source code for ATM program is given below:-
#include<stdio.h>
#include<conio.h>
void main()
{
char pass;
int balance =5000,wid,deposit,choice;
clrscr();
printf("\n enter your password");
scanf("%c",&pass);
if(pass == 'a' || pass == 'A')
{
printf("\n press 1 for show balance");
printf("\n press 2 for withdraw");
printf("\n press 3 for deposit");
printf("\n \t enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n your current balance is =%d",balance);
break;
case 2:
printf("\n enter your withdraw amount");
scanf("%d",&wid);
if(wid>balance)
{
printf("\n sorry apke khate me proper paisa nahi hai");
}
else
{ printf("collect your cash =%d your remainig balance is %d'",wid,balance-wid);
}
break;
case 3:
printf("enter youe deposit amount");
scanf("%d",&deposit);
printf("\n your update bal is =%d",balance = balance+deposit);
break;
default:
printf("\n incorrect choice bet 1 to 3 ");
}
}
else
{
printf("\n sorry pass is incorrect");
}
getch();
}
In switch we use case for matching the given variable. The switch statement test the value of given variable in a list of case values and when a match is found a block of statements associated with that case is executed.
ATM program on Switch statement-
Program perform the ATM operation Password? then check
1. Mini statement
2. Withdraw Amount
3. Deposit Amount.
Source code for ATM program is given below:-
#include<stdio.h>
#include<conio.h>
void main()
{
char pass;
int balance =5000,wid,deposit,choice;
clrscr();
printf("\n enter your password");
scanf("%c",&pass);
if(pass == 'a' || pass == 'A')
{
printf("\n press 1 for show balance");
printf("\n press 2 for withdraw");
printf("\n press 3 for deposit");
printf("\n \t enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n your current balance is =%d",balance);
break;
case 2:
printf("\n enter your withdraw amount");
scanf("%d",&wid);
if(wid>balance)
{
printf("\n sorry apke khate me proper paisa nahi hai");
}
else
{ printf("collect your cash =%d your remainig balance is %d'",wid,balance-wid);
}
break;
case 3:
printf("enter youe deposit amount");
scanf("%d",&deposit);
printf("\n your update bal is =%d",balance = balance+deposit);
break;
default:
printf("\n incorrect choice bet 1 to 3 ");
}
}
else
{
printf("\n sorry pass is incorrect");
}
getch();
}
It is correct ....given perfect output
ReplyDeleteWhy don't you use %f for amount
ReplyDeleteatm can't give amounts in poin3
Delete