Calculator in C

By: Emiley J Viewed: 161 times  Printer Friendly Format    


This is a simple calculator written in C. To use this calculator, just type the number, followed by the operand and then by another number. The application will then display the result. After the result is displayed, the program goes into the loop which asks if they want to do another calculation. If y is pressed, another calculation can be carried out.

#include 

int main()
{
  double number1 = 1.0;
  double number2 = 2.0;
  char operation = '+';
  char answer = 0;



  switch(operation)
  {
    case '+':
      printf("= %lf\n", number1 + number2);
      break;

    case '-':
      printf("= %lf\n", number1 - number2);
      break;

    case '*':
      printf("= %lf\n", number1 * number2);
      break;

    case '/':
      if(number2 == 0)
        printf("\n\n\aDivision by zero error!\n");
      else
        printf("= %lf\n", number1 / number2);
      break;

    case '%':
      if((long)number2 == 0)
        printf("\n\n\aDivision by zero error!\n");
      else
        printf("= %ld\n", (long)number1 % (long)number2);
      break;

    default:
      printf("\n\n\aIllegal operation!\n");
  }
}


Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Good work. Keep it up.
View Tutorial          By: pickatutorial at 2010-10-09 09:19:50

2. The program doesn't work as how it is described.
View Tutorial          By: Gurucharan at 2011-02-03 09:21:18

3. hi ,c language use for business man,wast for p
View Tutorial          By: p rambabu rddy at 2011-10-05 08:58:34

4. Its not working ave made a few adjustments to it t
View Tutorial          By: amg at 2011-11-04 13:48:09

5. the 2 not required lines
1) #include

View Tutorial          By: saurabh at 2012-02-20 06:33:57

6. Thanks for sharing. But I have made my own simple
View Tutorial          By: RIo Eduardo at 2013-02-18 02:39:08

7. this is very bad program because this is not run a
View Tutorial          By: danish at 2016-07-07 05:34:36