Introduction: Making a Calculator Using C Code
Welcome to what probably will be your first code ever, you will write an easy program that creates a simple calculator using the programming language "C".
Note: If images are to far or to close, please click on them to view the full image.
Step 1: Tips!
-Please keep in mind that starting to code may look like copy and paste to you but that is actually how most programmers start! Just go back and forth if you need to in order to understand what you are typing.
-Also, beware of misplacing functions, brackets, words, etc! As the compiler will give out an error if one thing is out of place.
-Please read the steps carefully! As you will get more information as you progress.
-Click on the pictures to see the whole code as you probably won't be able to see the whole picture.
Step 2: Look for the Online Compiler
There are numerous online compilers out there or you might even have your own downloaded, but for people who are new to coding, an online compiler is the best choice. We chose:
Or you can search google for online compiler.
Step 3: Make Sure You Have the Right Programming Language
We will be using "C" as our language. At the top right of the website, you should be able to select from various programming languages. The default is set to "C" but if its in another language change it, as code from C will not work on other languages.
Step 4: Syntax
It could be compared to how you structure sentences in any spoken language, such as English. In order for the compiler (the program that reads the code) to process what you've written, you need to enter it in a way that is readable to it. Please look at the table above to get an idea of what each is.
Step 5: Loading the Library
Make sure your basic interface includes the #include library AND a main function. Without either of these, your code will not compile or run properly.
Step 6: Begin Coding the Calculator
Take a look at the whole program, you can come here and check the details if you're missing something.
Step 7: Set Up the Function
Similarly to the main function we saw before, set up a function called DoMath with 5 Parameters using IF-ELSE statements. Each parameter will be an int value (integer) and will be for operation choice and the two numbers being operated.
Step 8: If-Else Statements
Create an if else statement block for each Operation.Each if-else statement enables us to give options to the user depending on what they want, once they choose an operation, the program will run only that part. For this tutorial, we’ll be doing 5 operations. Add, Subtract, Multiply, Divide, and finding the remainder of the two numbers when they are divided.
Step 9: Operations Inside If-Else
In each if-block, complete the operation based on which operation was selected by the user. Make sure to return the answer at the end (this sends the result to the main function).
Step 10: Building the Interface
Now we have to create the user interface for the user. In the main function, we will ask the user to input two integer values on top of listing out and asking them to input their selection for which operation they wish to complete.
After the user has inputted two integers and selected the operation, you will need to pass those 3 int values into the DoMath function we previously created. Make sure you declare and initialize an int value in the main function because the DoMath function will return an int value.
Step 12: Print the Result
Finally, we will print out the value that was returned from the DoMath function
Step 13: Run & Compile the Program
In order to run and compile your code that you have recently written in OnlineGDB, just press the green button at the top left of the page. If you’re not using the same compiler, this step may vary.
Step 14: Check the Results!
When the program asks the user to input values, make sure you press enter after every submission. If you’re using OnlineGDB, your output from the program will be under the output tab in the compiler. Again, if you’re not using OnlineGDB, this may vary.