Thursday, August 18, 2011

Simple Calculator Using Switch Statement




Turbo C program that would accept two numbers and will accept only these characters: '+', '-', '*', '/' otherwise display "Invalid input".

After accepting those characters, the Program will perform the specific operation and the answer will be displayed.




Click Read More for the code:




#include<stdio.h>
#include <conio.h>
#define g gotoxy
#define p printf
#define s scanf

main()
{
clrscr();

int x,n1,n2,sum,diff,pro,quo;
char ch;

g(3,1);p("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ");
g(3,2);p("Enter First Number:");
g(3,3);p("Enter Second Number:");
g(3,6);p("<+>Add");
g(3,7);p("<->Subtract");
g(3,8);p("<+>Multiply");
g(3,9);p("</>Divide");
g(3,12);p("Enter Your Choice:");
g(21,12);p("[    ]");
g(3,17);p("The Answer Is: ");
g(3,19);p("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ");

g(23,2);s("%d",&n1);
g(25,3);s("%d",&n2);


g(23,12);
ch=getche();
switch(ch)
{
case'+':
sum=n1+n2;
g(17,17);p("%d",sum);
break;
case'-':
diff=n1-n2;
g(17,17);p("%d",diff);
break;
case'*':
pro=n1*n2;
g(17,17);p("%d",pro);
break;
case'/':
quo=n1/n2;
g(17,17);p("%d",quo);

break;
default:
g(17,17);p("invalid input");
}


getch();
return 0;
}

1 comments:

Watch Miss Universe 2011

Watch live streaming video from missuniverse at livestream.com

Play Games - Fantastic Contraption

Advertise Here

New Post