(Learn C Programming for Beginners — Day 7)
Congratulations! 🎉 You’ve made it to Day 7 of your 30-Day C Programming Adventure!
You’ve survived the first week. You’ve gone from "What is a compiler?" to writing code that can talk, listen, and do math. That is a huge achievement.
Today is a Milestone Day. We aren't learning new syntax today. Instead, we are going to:
1. Review everything we've learned in Week 1.
2. Build a Mini Project — A fully functional Simple Calculator.
3. Celebrate your progress!
🔄 Week 1 Recap: The Foundation
Let's quickly look back at the tools you've added to your belt:
- Day 1 & 2: You set up your environment (VS Code + GCC) and wrote the legendary
Hello, World!. - Day 3: You learned about Variables (
int,float,char) — the boxes that hold your data. - Day 4: You mastered Input/Output (
printf,scanf) — making your programs interactive. - Day 5: You unlocked Operators (
+,-,%,==,&&) — the math and logic engines. - Day 6: You learned Code Style — writing clean, readable code with comments and proper indentation.
You have all the ingredients. Now, let's cook.
🛠️ Mini Project: The Simple Calculator
We are going to build a program that asks the user for two numbers and then performs addition, subtraction, multiplication, and division on them.
The Plan:
- Greet the user.
- Ask for two numbers.
- Perform calculations (Sum, Difference, Product, Quotient, Remainder).
- Display the results neatly.
Step 1: Create the File
Create a new file named day7_calculator.c.
Step 2: Write the Code
Try to write it yourself first! If you get stuck, here is the solution:
/*
Project: Simple Calculator (Week 1 Milestone)
Author: [Your Name]
Date: [Today's Date]
Description: A program that takes two integers and performs basic arithmetic.
*/
#include <stdio.h>
int main() {
// 1. Declare Variables
int num1, num2;
int sum, diff, prod, rem;
float quot; // Quotient needs to be a float for decimal precision
// 2. Input
printf("====================================\n");
printf("\tSIMPLE CALCULATOR v1.0\n");
printf("====================================\n\n");
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// 3. Calculations
sum = num1 + num2;
diff = num1 - num2;
prod = num1 * num2;
// Typecasting: We convert one integer to float to get a decimal result
quot = (float)num1 / num2;
rem = num1 % num2;
// 4. Output
printf("\n--- Results ---\n");
printf("Addition:\t%d + %d = %d\n", num1, num2, sum);
printf("Subtraction:\t%d - %d = %d\n", num1, num2, diff);
printf("Multiplication:\t%d * %d = %d\n", num1, num2, prod);
printf("Division:\t%d / %d = %.2f\n", num1, num2, quot);
printf("Modulus:\t%d %% %d = %d\n", num1, num2, rem); // Note: %% prints a single %
printf("\nThank you for using the calculator!\n");
return 0;
}
Step 3: Compile and Run
gcc day7_calculator.c -o calculator./calculator
🧠 Key Concepts in Action
-
Typecasting: Did you notice
(float)num1?- If we just did
num1 / num2(e.g.,5 / 2), C would do integer division and give us2. - By putting
(float)in front, we force C to treatnum1as a decimal number (5.0), so the result becomes2.5. This is a pro tip!
- If we just did
-
Escape Sequences: We used
\tto align the text and\nfor spacing. -
Printing
%: To print the actual percent symbol inprintf, you have to type it twice:%%.
🚀 Challenge: The "Tip Calculator"
Want to push yourself further? Modify your calculator to be a Tip Calculator.
1. Ask for the Bill Amount (float).
2. Ask for the Tip Percentage (int).
3. Calculate the Tip Amount (bill * percentage / 100).
4. Calculate the Total Bill (bill + tip).
5. Print the receipt.
🔮 What's Next? Week 2 Preview
You've mastered the basics. Next week, we give your programs a brain.
- Week 2 is all about Control Flow.
- You'll learn how to make your code make decisions (
if/else). - You'll learn how to repeat tasks (
loops) without writing the same code 100 times.
Get some rest, celebrate your Week 1 victory, and I'll see you on Day 8!
Found this helpful? Clap 👏 and follow to keep up with the adventure!
Comments
Post a Comment