Calculator in C
By: Emiley J 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");
}
}
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations 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
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