Making a Calculator Using C Code

by Cesar0910 in Circuits > Software

4832 Views, 3 Favorites, 0 Comments

Making a Calculator Using C Code

INS6(1.2).PNG
INS6(2.2).PNG
ImageCode.png

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.

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.

Look for the Online Compiler

Capture.PNG
INS2.PNG

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:

https://www.onlinegdb.com/online_c_compiler

Or you can search google for online compiler.

Make Sure You Have the Right Programming Language

INS3.PNG

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.

Syntax

LASR.PNG

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.

Loading the Library

INS7.PNG

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.

Begin Coding the Calculator

INS6(1.2).PNG
INS6(2.2).PNG

Take a look at the whole program, you can come here and check the details if you're missing something.

Set Up the Function

INS(FUNCTION1.2).PNG
INS(FUNCTION2.2).PNG

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.

If-Else Statements

IF-ELSE.PNG
EXP2.jpg

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.

Operations Inside If-Else

If-else1.1.PNG
If-else1.2.PNG
If-else1.3.PNG
If-else1.4.PNG
If-else1.5.PNG

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).

Building the Interface

MAIN.PNG

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.

MAIN2.PNG

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.

Print the Result

MAIN3.PNG

Finally, we will print out the value that was returned from the DoMath function

Run & Compile the Program

run.PNG

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.

Check the Results!

output.PNG

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.