Calculator in C

By: Emiley J Emailed: 1598 times Printed: 2044 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
UNIX read and write system calls sample program in C
Constants and escape sequences in C
A Storage Allocator sample program in C
ERRNO.H Header File in C
Calculator in C
Passing double value to a function in C
Printing a simple histogram in C
Getting Started with C
File Copying in C
Pointers and Arrays in C
Address Arithmetic and pointers in C
Table Lookup - hashtab - example program in C
Using printf function in C
fgets(), fputs() - Line Input and Output - sample program in C
Most Emailed Articles (in C)
Conditional Expressions in C
fgets(), fputs() - Line Input and Output - sample program in C
Symbolic Constants using #define in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Address Arithmetic and pointers in C
Character Pointers and Functions in C
Initialization of Pointer Arrays in C
Pointers to Structures example program in C
Using printf function in C
A Storage Allocator sample program in C
Using free() Function in C
The C Character Set
Writing The First C program
Formatting with printf in C
while, do while and for loops in C