// Simulate calculator using if else ladder
The If-Else Ladder is used if we have a multiple conditions to check.The same problem can be solved using switch case also.
The If-Else Ladder is used if we have a multiple conditions to check.The same problem can be solved using switch case also.
In this ladder first condition is checked using only if statement.If all conditions fail then last else will get executes
- int main()
- {
- int a,b,c;
- float r1;
- int flag=0;
- char ch;
- clrscr();
- printf("Enter x to stop");
- do
- {
- printf("enter the two numbers");
- scanf("%d%d",&a,&b);
- printf("\n enter any +,-,*,/");
- scanf(" %c",&ch);
- if(ch=='+')
- c=a+b;
- else if (ch=='-')
- c=a-b;
- else if(ch=='*')
- c=a*b;
- else
- {
- r1=(float)a/b;
- flag=1;
- }
- if(flag==1)
- printf("result is %f",r1);
- else
- printf("result is %d",c);
- } while(ch!='x');
- getch();
- }
Calculator program in C using switch
ReplyDeleteCheck above program. which does addition, subtraction divide and modulus in C using switch case statements.