Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Program to Display Mirror Image of a Number

    C Programming Examples


    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

    1. Initialize Variables: Create variables for the original number, reversed number, and temporary storage.
    2. Input Number: Take an input integer from the user.
    3. Loop and Reverse: Use a while loop to isolate and reverse each digit of the number.
    4. 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.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.