C Program to Reverse a Number

Introduction

Reversing a number involves reversing the order of its digits. This guide will show you how to write a C program to reverse a number provided by the user.

Problem Statement

Create a C program that:

  • Takes an integer input from the user.
  • Reverses the digits of the number.
  • Displays the reversed number.

Example:

  • Input: 1234
  • Output: 4321

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the original number, the reversed number, and the remainder.
  4. Input the Number: Use scanf to take input from the user for the number.
  5. Reverse the Number: Use a loop to reverse the digits of the number.
  6. Display the Reversed Number: Use printf to display the reversed number.

C Program

#include <stdio.h>

/**
 * C Program to Reverse a Number
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the original number, reversed number, and remainder
    int num, reverse = 0, remainder;

    // Step 2: Prompt the user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Step 3: Reverse the number using a loop
    while (num != 0) {
        remainder = num % 10;           // Step 3.1: Get the last digit
        reverse = reverse * 10 + remainder; // Step 3.2: Append the last digit to the reversed number
        num = num / 10;                 // Step 3.3: Remove the last digit from the original number
    }

    // Step 4: Display the reversed number
    printf("Reversed number: %d\n", reverse);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable num is declared to store the original number entered by the user. reverse is initialized to 0 and will hold the reversed number. remainder is used to store the last digit of the number during each iteration of the loop.

Step 2: Input the Number

  • The program prompts the user to enter an integer using printf. The scanf function then reads the input and stores it in the variable num.

Step 3: Reverse the Number Using a Loop

  • The program uses a while loop to reverse the digits of the number:
    • Step 3.1: The remainder when num is divided by 10 gives the last digit of num.
    • Step 3.2: This last digit is then added to the reverse variable after multiplying reverse by 10 to shift its digits to the left.
    • Step 3.3: The original number num is divided by 10 to remove its last digit. This process repeats until num becomes 0.

Step 4: Display the Reversed Number

  • The program displays the reversed number using the printf function.

Step 5: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter an integer: 1234
Reversed number: 4321

Example 2:

Enter an integer: 98765
Reversed number: 56789

Example 3 (Single Digit):

Enter an integer: 7
Reversed number: 7

Conclusion

This C program demonstrates how to reverse the digits of a number entered by the user. It covers basic concepts such as loops, arithmetic operations, and handling user input, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top