To understand this example, you should have the knowledge of the following C programming topics:. Then, it performs the calculation on the two operands depending upon the operator entered by the user. And, the two operands, 1. Course Index Explore Programiz.
C program to make calculator using switch case
Popular Tutorials Data Types in C. C for Loop. Arrays in C Programming. Pointers in C. Find roots of a quadratic equation. Print Pyramids and Patterns. Check prime number. Print the Fibonacci series. Reference Materials string. Start Learning C. Explore C Examples. Check Whether a Character is a Vowel or Consonant. Find the Roots of a Quadratic Equation. Check Whether a Number is Positive or Negative.
Check Whether a Character is an Alphabet or not. Calculate the Sum of Natural Numbers. Find Factorial of a Number. Generate Multiplication Table. Join our newsletter for the latest updates.
You have successfully subscribed to C newsletter. C Programming. Share on:. Was this article helpful? Sorry about that How can we improve it? Related Examples. C Example Compute Quotient and Remainder. C Example Add Two Integers.Following is a simple example that shows declaration and function call using function pointer. See following post for details. How to declare a pointer to a function? Typically a function pointer stores the start of executable code.
Below example in point 5 shows syntax for array of pointers. For example, in below program, user is asked for a choice between 0 and 2 to do different tasks. For example, consider the following C program where wrapper receives a void fun as parameter and calls the passed function.
This point in particular is very useful in C. In C, we can use function pointers to avoid code redundancy. For example a simple qsort function can be used to sort arrays in ascending order or descending or by any other order in case of array of structures.
Not only this, with function pointers and void pointers, it is possible to use qsort for any data type. Similar to qsortwe can write our own functions that can be used for any data type and can do different tasks without code redundancy. Below is an example search function that can be used for any data type.
In fact we can use this search function to find close elements below a threshold by writing a customized compare function. The above search function can be used for any data type by writing a separate customized compare. For example virtual functions. Class methods are another example implemented using function pointers. Refer this book for more details. This article is contributed by Abhay Rathi.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Become industry ready at a student-friendly price.
Writing code in comment? Please use ide. Output of the program Dereference, Reference, Dereference, Reference…. And we can use the same qsort. Note that void pointers are used so that. Recommended Posts: How to declare a pointer to a function?C Language. Post a comment. Also, in a different example, you can generate Fibonacci series up to a certain number, like or 50 whatever you want. In Fibonacci series, the next number is the sum of previous two numbers, For Example : 0, 11, 2, 3, 5, 8, 13, 21 and so on.
C Programs for Fibonacci Series 1. Read more. Here's the C Program to convert temperature from Fahrenheit to Celsius degree centigrade. How this Program Work? First, We will take the value from the user in Fahrenheit and convert it into Celsius. Then, we'll apply the formula to convert Fahrenheit into Celsius. Then, we'll print the value in Celsius. In this example, we will learn how you can get two values from a function in C using pointers. For this example, we're making a function of quadratic formula to return multiple values because the quadratic formula has two significant one positive and one negative.
In function, we can only get one value in the main method with the return, so for the second value we will use a pointer How to Get Multiple Values From a Function Using Pointers.To create this article, volunteer authors worked to edit and improve it over time.C++ programming of simple calculator using functions
Tips and Warnings. Things You'll Need. Related Articles. Co-authored by 6 contributors Community of editors, researchers, and specialists March 25, Create source file. Declare Headers. Begin writing in source file, using include declare headers iostream and iomanip and using the identifier, using namespace declare std.
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. Unless you'll be maintaining an accumulated number for multiple operations, this may not be the best use of classes. For your code, you'd get the same effect by just performing these calculations within the switch statement something similar to this.
Right now, you're just using a class to contain similar functions, including a trivial output function. Move it outside switch or to another method. I'm aware it's just a learning example but to be honest the usage of class object here is completely pointless. The first thing I notice is that you ask for numbers 1 and 2 in every case statement. You know will always need two numbers, why not move that outside and code it once?
Others have already pointed out that this is a contrived example, but no one has mentioned this angle which actually prompted me to make my first post here For me it's a bit "brittle" because your main's menu is linked to the operations in the class. If you modify the functions class to add some more operations you also have to modify main to put them in the menu and get the parameters to pass through.
One approach would be to let functions print the menu - but then it also needs to map responses to functions, so then you might say make it get the parameters as well, so you run the risk of creating a "ball of mud". In fact you can basically get rid of the switch block altogether, by using a map to store a pointer the appropriate function with the character of the operation as the key.
Now if you make the menu choice the operation characters, just get the 2 operands and run the appropriate function. Here's a little snippet that demonstrates how to set up and use the map:. There are some good answers here. My two cents, keeping my suggestions small and simple regarding the class is:. Sign up to join this community.
C/C++ program to make a simple calculator
The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 3 years, 8 months ago. Active 3 years, 7 months ago. Viewed 27k times. What can I improve on? Taylor Robinson-Powell Taylor Robinson-Powell 2 2 gold badges 2 2 silver badges 7 7 bronze badges.
If this is what you want then fine, otherwise you should change the type of this function to double. Your class actually serves no purpose except to group functions — so use a namespace instead!
Active Oldest Votes. Some additional notes: Try not to use using namespace std. Indent everything within main as well. Not doing so may make it hard to tell that the code is being contained within something.In this example, you will gain step by step insight about C program to make calculator using switch case.
It is easy to implement the simple function of a calculator in C programming. The following code will have the simple function as addition, subtraction, multiplication, division and, square. Please go through following articles of C programming to understand the concept of C program to make a simple calculator using switch case. This is simple C program to make a calculator using switch case.
We have 5 different cases for selecting mathematical operation. There is a special case for the division. If the user enters zero in the divisor, the program asks for another divisor and quotient is displayed. Search for: Search. Skip to content. More C Examples. C program to read a character from keyboard and print it in reverse case i.
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have tried making simple calculator in C. I am new in programming, so pardon for any silly mistake.
Subscribe to RSS
Is there any problem of parameters, as I have taken 3 input but in parameters of function I have used 2 only. Divide Function is not working well. This integer is then assigned to divide which is type float, at this point the integer result of the division is converted to a float.
You need to change the value that the functions return to void because they don't return anything. You roughly want this, but there is still a lot of room for improvement e. Learn more. How to make simple calculator using Functions in C? Ask Question. Asked 4 years ago.
Active 4 years ago. Viewed 3k times. What is the error that you get?
What is it doing, and how is that different than what you're expecting? I can confidently say it is highly unlikely that is what you desire.
And why you're passing those arguments by address remains a mystery. You probbaly want to replace all int s by float s or double s in order to be able to use floating point numbers.
You need to get a modern compiler or learn how to configure your current one. Active Oldest Votes. The other points made about your code are all good, and should also be addressed. Andrew Andrew 2, 10 10 silver badges 15 15 bronze badges. SouravGhosh so if you don't write them right - the main function won't know them - and can't use them!
Jabberwocky Jabberwocky Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.