## Sunday, 23 February 2014

### Basic Calculator using Switch() loop

When Anyone introduce to programming .He wants to make something productive . And calculator is easiest thing which you can make from your very basic programming . And today we gonna Do this ...

We are going to make first Calculator program in C .

It can do following operation addition, subtraction, Division, Multiplication and Power .
these are very basic operation
steps :

Including stranded header file stdio.h, Math.h , and stdlib.h .
declare variables
Now we make while loop with 1 as argument it means this loop gonna run infinite until we call break or exit   function . We use this to use program for many times until we want to stop .
Now take variable a and b as input on which we have to do operation .
than we have to enter Our choice of operation .depend on that switch loop choose operation .
when we choose operation for sum it goes to case 1
when we choose to exit it goes to case 6 to exit and program stop & Exit .

If someone choose a option other than these operation it goes to default i.e  print wrong choice!

#### C code :

`````` // http://beginer2cs.blogspot.com
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
void main ()
{
int index;
float a,b,c;
printf("Calculator.. ");
while(1)
{
printf("\nEnter two operands.. \n");
scanf("%f%f",&a,&b);
printf("\nMenu.. \n1. Addition \n2. Substraction \n3. Multiplication \n4. Division \n5 power \n6 to EXIT \n ");
printf("\n Enter choice : ");
scanf("%d",&index);
switch (index)
{
case 1:
c=a+b;
printf(" sum =%f",c);
break;
case 2:
c=a-b;
printf("Sub =%f",c);
break;
case 3:
c=a*b;
printf("multi =%f",a,b,c);
break;
case 4:
c=a/b;
printf("Div =%f",c);
break;
case 5:
printf("Pow = %f",pow(a,b));
break;
default:
case 6:
exit(0);
break;
printf("Wrong Choice");
}
}
}
``````

Have A nice DAY