- How to reverse a number using while loop in C
- How to reverse a number using do-while loop in C
- The difference between while and do-while loops
- How to extract digits using modulo and division operations
- Practice exercises to strengthen your understanding
Introduction
In this tutorial, we'll learn how to reverse a number using while and do-while loops in C. Reversing a number is a common programming problem that helps you understand loops, modulo operations, and division.
The main idea is simple: we extract the last digit of the number using the modulo operator (%),
build the reversed number by multiplying the existing reversed number by 10 and adding the extracted digit,
and then remove the last digit from the original number using division (/).
š” Key Concept: To reverse a number, repeatedly extract the last digit using n % 10,
append it to the reversed number, and remove it from the original number using n / 10.
1. Reverse Number Using While Loop
How to reverse a number with a while loop
In this program, we take a number input from the user and reverse it using a while loop. The loop continues as long as the number is not zero. Inside the loop, we extract the last digit, build the reversed number, and remove the last digit.
C Program Code to Reverse a Number Using While Loop
#include <stdio.h>
int main() {
int n, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
printf("Reversed number: %d\n", rev);
return 0;
}
Output
Enter a number: 12345
Reversed number: 54321
How This Program Works
When you run this program, it declares three integer variables: n (the input number),
rev (initialized to 0, to store the reversed number), and rem (to store the remainder).
The computer displays "Enter a number:" on the screen and pauses at the scanf function until you type a value.
If you enter 12345, that value is saved directly into n.
Next, the execution path reaches the while loop and tests the condition n != 0.
Since n is 12345, the test passes, and the program moves inside the loop block.
Inside the loop, it performs three operations:
rem = n % 10ā extracts the last digit (e.g., 12345 % 10 = 5)rev = rev * 10 + remā appends the digit to the reversed numbern = n / 10ā removes the last digit from the original number
This cycle repeats until n becomes 0. On each iteration, we extract, build, and remove.
Finally, the loop stops, and the program prints the reversed number.
2. Reverse Number Using Do-While Loop
How to reverse a number with a do-while loop
The second program uses a do-while loop to reverse a number. The do-while loop ensures that the reversal process runs at least once, even if the input number is zero. The logic for reversing the number remains the same.
C Program Code to Reverse a Number Using Do-While Loop
#include <stdio.h>
int main() {
int n, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
do {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
} while(n != 0);
printf("Reversed number: %d\n", rev);
return 0;
}
Output
Enter a number: 9876
Reversed number: 6789
How This Program Works
When you run this program, it initializes the variables and prompts you to enter a number.
If you enter 9876, that value is stored in n.
Next, the execution path enters the do block directly without checking any conditions upfront.
It performs the same three operations: extracts the last digit, appends it to the reversed number, and removes it from the original number.
After completing the first pass, the program checks the condition while(n != 0) at the bottom.
If the condition is true, it jumps back to the top of the loop. This cycle repeats until n becomes 0.
The key difference between while and do-while is that the do-while loop guarantees at least one iteration, making it useful when you want the loop to execute even if the condition is initially false.
Practice Exercises
š Exercise 1: Reverse a Number with Leading Zeros
What happens if you reverse a number like 1000? Try to modify the program to handle this case.
Show Solution
#include <stdio.h>
int main() {
int n, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
int original = n;
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
printf("Original: %d\n", original);
printf("Reversed: %d\n", rev);
return 0;
}
š Exercise 2: Check if a Number is a Palindrome
Modify the program to check if the original number is equal to its reverse (palindrome).
Show Solution
#include <stdio.h>
int main() {
int n, rev = 0, rem, temp;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(temp != 0) {
rem = temp % 10;
rev = rev * 10 + rem;
temp = temp / 10;
}
if(n == rev)
printf("%d is a palindrome\n", n);
else
printf("%d is not a palindrome\n", n);
return 0;
}
Frequently Asked Questions
How does a C program reverse a number using a while loop?
To reverse a number using a while loop in C, the program takes a number input from the user, initializes rev = 0, and runs the loop as long as n != 0. Inside the loop, it extracts the last digit using n % 10, builds the reversed number using rev = rev * 10 + rem, and removes the last digit using n = n / 10.
What is the difference between while and do-while when reversing a number?
The while loop checks the condition first, so if the number is 0, it won't execute. The do-while loop executes at least once, which can be useful when you want to handle the number 0 as input.
Why do we use modulo (%) and division (/) to reverse a number?
Modulo (%) extracts the last digit, and division (/) removes the last digit. This process repeats until all digits are processed.
Can we reverse a number without using loops?
Yes, you can use recursion or convert the number to a string and reverse it, but loops are the most efficient and commonly used method in C.
š” Tip: Always store the original number in a temporary variable if you need to preserve it for later use.
šÆ Key Takeaway: Reversing a number using loops is a fundamental programming skill. It helps you understand how to extract and manipulate digits, which is useful for many other problems like checking palindromes, finding Armstrong numbers, and more.
š„ C While & Do-While Loop Programs (Practice Set 1)
š Practice the most important C while loop and do-while loop programs asked in exams and interviews. These beginner-to-advanced problems will help you master loop concepts quickly.
-
C Program to Print Odd Numbers from 1 to N (While & Do-While)
Learn how to print odd numbers using loops in C. A basic problem to understand loop iteration.
-
C Program to Print Even Numbers from 1 to N
Understand how to display even numbers using while and do-while loops in C.
-
C Program to Print Uppercase Alphabets (A to Z)
Print all uppercase letters using loops. Helps in understanding ASCII values and iteration.
-
C Program to Print Lowercase Alphabets (a to z)
Simple loop program to print lowercase alphabets in C.
-
C Program to Print Numbers from 1 to 10
Basic beginner example to understand number printing using loops.
-
C Program to Print Multiplication Table using While Loop
Take user input and generate a multiplication table. Common interview question.
-
C Program to Check Positive, Negative or Zero
Check number type continuously using loops until user exits.
-
C Program to Find Factorial using While Loop
Calculate factorial using loops. Important for coding interviews.
-
C Program to Find Sum of First N Natural Numbers
Compute sum from 1 to N using loop logic. Core beginner problem.
-
C Program to Print Prime Numbers from 1 to N
Learn prime number logic using nested loops in C programming.
-
C Program to Print Armstrong Numbers from 1 to N
Understand Armstrong number logic using loops and mathematical operations.
-
C Program to Print Leap Years using While Loop
Find leap years using conditions and loops. Useful for real-world logic building.
-
C Program to Reverse a Number using While Loop
Reverse digits of a number using loops. Frequently asked interview question.
š Practice/Assignment Set 2: C While & Do-While Loop Series Programs
1. C Program to Find Sum of Squares from 1 to N using While Loop
Learn how to calculate the sum of squares (1² + 2² + 3² + ... + N²) using while and do-while loops in C. This is a commonly asked logic-building problem in exams and coding interviews.
2. C Program to Find Sum of First N Natural Numbers using While Loop
Write a C program to calculate the sum of natural numbers (1 + 2 + 3 + ... + N) using loops. A basic and important program for beginners learning loop concepts.
3. C Program to Find Sum of Series (1/1! + 2/2! + ... + N/N!)
Practice a slightly advanced loop problem by computing the sum of factorial-based series using while or do-while loops. Helps strengthen logic and mathematical programming skills.
4. C Program to Find Sum of Harmonic Series (1 + 1/2 + 1/3 + ... + 1/N)
Learn how to calculate the harmonic series using loops in C. This program improves understanding of floating-point calculations and loop control.
5. C Program to Find Sum of Series (1 + 3²/3³ + 5²/5³ + ... up to N Terms)
Solve an advanced series-based problem using while loop logic. This type of question is frequently asked in competitive programming and technical interviews.
Other Tutorials
SQL FAQ
Java FAQ
Python FAQ