Build a Simple Calculater in C – Easy Peasy for Newbies

Build a Simple Calculater in C – Easy Peasy for Newbies

introducton

Hey, you! Yeah, you—the one who’s just starting out with C and wondering what the heck you’re doing. Today, we’re gonna build a super simple calculator. I mean, who doesn’t love calculators? They’re on your phone, that fancy graphing thing from school, even that cheap one your mom keeps in the kitchen drawer. This lil’ program’s gonna add, subtract, multiply, and divide two numbers. It’s perfect for beginners, and trust me, I had a blast messing with this when I first learned C. Let’s get cracking!

c program

Here’s the code—nothing too crazy, just a basic calculator. I stuck it in a snazzy lil’ box so it looks proper on your blog. Check it!

#include

int main() {
float num1, num2, result;
char operator;

printf("enter first number: ");
scanf("%f", &num1);

printf("enter an operator (+, -, *, /): ");
scanf(" %c", &operator);

printf("enter second number: ");
scanf("%f", &num2);

if (operator == '+') {
result = num1 + num2;
printf("%.2f + %.2f = %.2f\n", num1, num2, result);
}
else if (operator == '-') {
result = num1 - num2;
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
}
else if (operator == '*') {
result = num1 * num2;
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
}
else if (operator == '/') {
if (num2 != 0) {
result = num1 / num2;
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
}
else {
printf("error! cant divide by zero, mate.\n");
}
}
else {
printf("whoops! that’s not a real operator.\n");
}

return 0;
}

output

Here’s what pops up when you run it. I threw in a chill example and one where I goofed up (dividing by zero—classic me!). Take a peek:

enter first number: 7.8
enter an operator (+, -, *, /): *
enter second number: 2.5
7.80 * 2.50 = 19.50

enter first number: 10
enter an operator (+, -, *, /): /
enter second number: 0
error! cant divide by zero, mate.

explaination

Okay, let’s chat about this code like we’re sitting at a café or something. I’ll break it down nice and easy—imagine I’m showing you my messy notebook from when I first learned this stuff. C can feel like a puzzle, but once you get it, it’s SO COOL. Here we go!

1. That `#include ` bit
This is like grabbing a toolbox before you start building. The `` thingy—it’s short for “standard input-output”—lets us use `printf` and `scanf`. Without it, C’s like, “Uh, what are those?” I forgot it once, and my code just sat there sulking.

2. The `int main()` funtion
Every C program needs a “main” to kick things off—it’s like the front door to your house. The `int` means it’ll spit out a number at the end (we give it a `0` to say “all good!”), and those curly braces `{}` are basically the walls keeping everything inside. Simple, right?

3. Making some boxes: `float num1, num2, result; char operator;`
Variables are just little buckets to hold stuff. I used `float` cuz we’re dealing with decimals (like 7.8 or 2.5), not boring old whole numbers. Then `char operator` is for holding one lil’ character—like `+` or `*`. I named ‘em obvious stuff so I wouldn’t forget what’s what.

4. Chatting with the user: `printf` and `scanf`
So yeah, `printf("enter first number: ")` is me yelling, “Hey, gimme a number!” Then `scanf("%f", &num1)` grabs whatever you type and shoves it in `num1`. The `%f` says “expect a float,” and that `&` is like a sticky note saying “put it HERE.” Oh, and see the space in `scanf(" %c", &operator)`? That’s a sneaky fix I learned—it skips junk like extra enters so the operator doesn’t get confused.

5. The `if` and `else if` stuff
This is where it gets fun! We’re checking what operator you picked with `if (operator == '+')`. That `==` is C’s way of asking, “Is this the same as that?” If it’s a `+`, we add the numbers and show the result. Then `else if` keeps the party going for `-`, `*`, and `/`. It’s like a choose-your-own-adventure book—pick this, do that!

6. Watch out for division
Dividing by zero? Big nope. I learned that the hard way when my program crashed once. So, in the `/` part, we toss in `if (num2 != 0)`—that `!=` means “not zero.” If it’s safe, we divide; if not, we yell “error!” and save the day.

7. Pretty numbers with `%.2f`
When we print stuff like `printf("%.2f + %.2f = %.2f\n", num1, num2, result)`, the `%.2f` is my fave trick. It’s like, “Yo, show two decimals only!” So you get 19.50, not some ugly 19.5000001 mess. The `\n` just adds a new line—keeps it clean.

8. The “oops” catcher: `else`
What if you type something dumb like `&`? The last `else` is my backup—it’s like, “Dude, that’s not a thing!” and lets you know you goofed. Saved me tons of headaches.

9. Wrapping up with `return 0`
At the end, `return 0;` is me waving bye-bye to the program, saying, “We’re done, and it worked!” It’s just a C tradition—kinda like signing your name on a test.

And there ya go! You’ve got yourself a calculator that’s small but mighty. Mess with it—change the words, try goofy numbers, whatever! When I first wrote this, I spent HOURS figuring out that space in `scanf`, so don’t feel bad if it takes a sec to click. You’ve got this! 😊