At first glance, it may look similar with other loops, but the while loop shines when:
-
You don’t know in advance how many times a loop should run.
-
You want to keep looping until a condition becomes false.
-
You’re handling input, interactive menus, or problems where the end depends on data (not just a fixed count).
Let’s start from simple problems and gradually move to moderate ones, all in C, with code and walkthroughs.
🔹 1. Basics of While Loop
The structure of a while loop looks like this:
while(condition) { // code block}
👉 The loop keeps executing as long as the condition is true.🔹 2. Print Numbers from 1 to 5
Code:
#include <stdio.h>int main() { int i = 1; while(i <= 5) { printf("%d ", i); i++; } return 0;}
Walkthrough:
-
Start with
i = 1. -
Condition:
i <= 5→ true → printi. -
Increment
i. Loop stops when
i = 6.
🔹 3. Sum of First N Natural Numbers
Code:
#include <stdio.h>int main() { int n = 5, sum = 0, i = 1; while(i <= n) { sum += i; // add i to sum i++; } printf("Sum = %d", sum); return 0;}
👉 For n = 5, calculation is 1+2+3+4+5 = 15.
Output: Sum = 15
🔹 4. Reverse Digits of a Number
This is a classic use of while loop because we don’t know the number of digits beforehand.
Code:
#include <stdio.h>int main() { int num = 1234, rev = 0; while(num > 0) { int digit = num % 10; // extract last digit rev = rev * 10 + digit; // build reverse number num = num / 10; // remove last digit } printf("Reversed = %d", rev); return 0;}
Walkthrough:
-
Start:
num = 1234. -
Iteration 1 → digit = 4 → rev = 4 → num = 123.
-
Iteration 2 → digit = 3 → rev = 43 → num = 12.
-
Iteration 3 → digit = 2 → rev = 432 → num = 1.
-
Iteration 4 → digit = 1 → rev = 4321 → num = 0 → stop.
👉 Output: Reversed = 4321
🔹 5. Factorial of a Number
Code:
#include <stdio.h>int main() { int n = 5, fact = 1; while(n > 0) { fact *= n; n--; } printf("Factorial = %d", fact); return 0;}
👉 Calculation: 5*4*3*2*1 = 120.
Output:Factorial = 120
🔹 6. Check for Palindrome Number (Moderate)
A palindrome number reads the same backward and forward (like 121, 1331).
Code:
#include <stdio.h>int main() { int num = 1331, temp = num, rev = 0; while(num > 0) { int digit = num % 10; rev = rev * 10 + digit; num /= 10; } if(temp == rev) printf("Palindrome Number"); else printf("Not a Palindrome"); return 0;}
🔹 7. Menu-Driven Program (Moderate)
While loops are great for interactive programs like menus.
Code:
#include <stdio.h>int main() { int choice; while(1) { printf("\n--- MENU ---\n"); printf("1. Print Hello\n"); printf("2. Print Bye\n"); printf("3. Exit\n"); printf("Enter choice: "); scanf("%d", &choice);
if(choice == 1) printf("Hello!\n"); else if(choice == 2) printf("Bye!\n"); else if(choice == 3) break; // exit the loop else printf("Invalid choice!\n"); } return 0;}
--- MENU ---1. Print Hello2. Print Bye3. ExitEnter choice: 1Hello!
Enter choice: 2Bye!
Enter choice: 3(exit program)
🎯 Key Takeaways
-
While loop keeps running until condition becomes false.
-
Useful when you don’t know how many times to loop in advance.
-
Great for digit problems, condition-based tasks, and menus.
-
Common pitfalls: forgetting to update variable → infinite loop.
⚡ Challenge for You!
Try to solve this using a while loop:
👉 Write a program to find the sum of digits of a given number.
Example: Input: 987
Output: 24 (9+8+7)
Post a Comment