Calculator in C

By: Emiley J Emailed: 1728 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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");
  }
}

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

The program doesn't work as how it is described.

View Tutorial          By: Gurucharan at 2011-02-03 09:21:18
2. View Comment

Its not working ave made a few adjustments to it though and it okay now

View Tutorial          By: amg at 2011-11-04 13:48:09
3. View Comment

the 2 not required lines
1) #include
2) char answer = 0

Add a line will necessary to :-
return 0;


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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
Find square and square root for a given number in C
Error Handling - Stderr and Exit - sample program in C
UNIX read and write system calls sample program in C
Using free() Function in C
Formatting with printf in C
Symbolic Constants using #define in C
Character Arrays in C
Precedence and Order of Evaluation in C
Variable Scope and functions in C
register Variables in C
Initialization of Variables in C
Structures and Functions in C
Arrays of Structures example program in C
malloc, calloc - Storage Management - in C
Most Emailed Articles (in C)
Listing Files and Directories sample program in C
Character Arrays in C
Arithmetic Operators in C
Relational and Logical Operators in C
Type Conversions in C (String to Integer, isdigit() etc)
Increment and Decrement Operators in C
getch and ungetch in C
Variable Scope and functions in C
Static Variables in C
register Variables in C
File Inclusion in C
Pointers and Function Arguments in C
Pointers and Arrays in C
Character Pointers and Functions in C
Pointer Arrays and Pointers to Pointers in C