🔄 Loops + Conditionals in C: Solving Real Problems Step by Step

 



By now, you know that:

  • Conditionals (if, else) help your program decide.
  • Loops (for, while, do-while) help your program repeat.

But here’s the real magic: when you combine them, you can solve actual problems — from simple checks to real-world mini systems.

Let’s dive into three hypothetical problems (easy, moderate, complex), each solved with code + explanation.


🟢 Easy Problem: Count How Many Numbers Are Even

Problem: You’re given numbers from 1 to 10. Count how many of them are even.



#include <stdio.h>
int main() {
int count = 0;
for(int i = 1; i <= 10; i++) {
if(i % 2 == 0) {
count++;
}
}
printf("Total even numbers between 1 and 10 = %d\n", count);
return 0;
}



💡 Explanation:

  • The for loop runs numbers 1 → 10.
  • The if statement checks if a number is even (i % 2 == 0).
  • Every time it’s true, we increase count.
  • At the end, we print the result.

👉 Loops helped us repeat checking for 10 numbers.
👉 Conditionals helped us decide which were even.

Output:

Total even numbers between 1 and 10 = 5

🟡 Moderate Problem: Simple Password System with 3 Attempts

Problem: Create a login system. The user gets 3 chances to enter the correct password. If correct → success. If not → access blocked.



#include <stdio.h>
#include <string.h>
int main() {
char password[20];
int attempts = 0;
while(attempts < 3) {
printf("Enter password: ");
scanf("%s", password);
if(strcmp(password, "Cprogram") == 0) {
printf("Login successful 🎉\n");
return 0; // Exit program after success
} else {
printf("Wrong password! Try again.\n");
attempts++;
}
}
printf("Access blocked after 3 failed attempts ❌\n");
return 0;
}



💡 Explanation:

  • The while loop allows up to 3 tries.
  • Each time, the program checks (if) if the entered password matches "Cprogram".
  • If correct → login success + exit program.
  • Else → retry until attempts run out.

👉 Loops give multiple chances.
👉 Conditionals decide if access is granted or denied.


🔴 Complex Problem: ATM Cash Withdrawal Simulation

Problem: Build a simple ATM program where:

  • User has ₹10,000 in balance.
  • They can withdraw money multiple times.
  • If withdrawal is greater than balance → show error.
  • If withdrawal is valid → deduct from balance.
  • Program ends when balance < 100 or user chooses exit.


#include <stdio.h>
int main() {
int balance = 10000;
int withdraw;
char choice;
do {
printf("Current Balance: ₹%d\n", balance);
printf("Enter amount to withdraw: ");
scanf("%d", &withdraw);
if(withdraw > balance) {
printf("Insufficient funds ❌\n");
} else if(withdraw <= 0) {
printf("Invalid amount ❌\n");
} else {
balance -= withdraw;
printf("Withdrawal successful ✅ New Balance: ₹%d\n", balance);
}
if(balance < 100) {
printf("Balance too low. ATM service ending ❌\n");
break;
}
printf("Do you want another transaction? (y/n): ");
scanf(" %c", &choice);
} while(choice == 'y' || choice == 'Y');
printf("Thank you for using ATM 🏦\n");
return 0;
}


💡 Explanation:

  • do-while ensures the ATM runs at least once.
  • if-else checks multiple conditions:
    • More than balance → error.
    • Zero or negative → error.
    • Valid → withdraw money.
  • Balance check ensures program exits when funds are low.
  • User can choose to continue or exit.

👉 This combines loops + nested conditionals in a practical way, like a real ATM!


🎯 Assignments for You

Try solving these with loops + conditionals:

  1. Multiplication Table Generator: Print multiplication table of a number (e.g., 5 × 1 to 5 × 10).
  2. Guess the Number Game: Computer has a number (say 42). User keeps guessing until they get it right, with hints (“too high”, “too low”).
  3. Grade Calculator: Take marks of 5 subjects, calculate average, and assign grade (A/B/C/Fail).
  4. Digital Clock Simulation: Print time in hh:mm:ss format from 00:00:00 to 23:59:59 using loops.
  5. Prime Numbers Between 1–100: Use loops to check and print all prime numbers.

🚀 Final Words

Loops and conditionals are like the Batman & Robin of C programming 🦇 — one repeats tasks, the other makes decisions. Together, they help your code solve problems that actually feel alive.

👉 Next time you’re stuck, think:

  • Do I need to repeat something? → Use a loop.
  • Do I need to make a choice? → Use a conditional.
  • Do I need both? → Congratulations, you’re solving a real problem!

 







Post a Comment

Previous Post Next Post