Creating a C program to display the mirror image of a number is a typical task for both beginners and intermediate programmers. This tutorial will guide you through the process, complete with examples and explanations so you can learn this simple yet impactful program.
What is a Mirror Image of a Number?
The mirror image of a number is the number obtained by reversing its digits. For example, the mirror image of 123 is 321.
Algorithm to Mirror Image a Number
- Initialize Variables: Create variables for the original number, reversed number, and temporary storage.
- Input Number: Take an input integer from the user.
- Loop and Reverse: Use a
while
loop to isolate and reverse each digit of the number. - Output Result: Display the reversed number, which is the mirror image.
Code Example
Here's a C program that displays the mirror image of a number using the above algorithm:
#include <stdio.h>
int main() {
int number, reverse = 0, remainder;
// Take user input
printf("Enter an integer: ");
scanf("%d", &number);
// Reverse the number
while(number != 0) {
remainder = number % 10;
reverse = reverse * 10 + remainder;
number /= 10;
}
// Display the result
printf("Mirror image of the number is: %d\n", reverse);
return 0;
}
Program Output:
Assume you entered the number 1234. The program will loop through each digit, reverse them, and then output 4321, the mirror image of the original number.
Conclusion
Writing a C program to display a mirror image of a number is simple. You must initialize variables, enter a number, reverse it using a loop, and display the result. Following this tutorial, you can easily write a C program that displays the mirror image of a number.