TechTorch

Location:HOME > Technology > content

Technology

Reverse a 4-Digit Number in C Programming: A Comprehensive Guide

April 03, 2025Technology1275
How to Write a C Program for Reversing a 4-Digit Number To write a C p

How to Write a C Program for Reversing a 4-Digit Number

To write a C program that reverses a 4-digit number, you need to follow a few steps. This tutorial will guide you through the process, starting from input validation to reversing the digits and finally printing the reversed number.

Program Overview and Key Concepts

The program will:

Read a 4-digit number from the user. Check if the input number is valid. Use a loop to extract each digit and build the reversed number. Output the reversed number to the user. Add an option to repeat the operation or exit the program.

Program Code

Example 1: Basic Reverse Program

The following C program takes a 4-digit number as input, reverses it, and outputs the result:

#include stdio.hint main() {    int num, reversedNum  0, remainder;    // Prompt the user for a 4-digit number    printf(Enter a 4-digit number: );    scanf(%d, num);    // Check if the number is a 4-digit number    if (num  1000 || num  9999) {        printf(Invalid input. Please enter a valid 4-digit number.
);        return 1; // Exit the program with an error code    }    // Reverse the number    while (num ! 0) {        remainder  num % 10; // Get the last digit        reversedNum  reversedNum * 10   remainder; // Build the reversed number        num / 10; // Remove the last digit from num    }    // Print the reversed number    printf(Reversed number: %d
, reversedNum);    return 0; // Exit the program successfully}

Example 2: Interactive Reverse Program

The following C program allows the user to reverse a 4-digit number and choose to repeat the process or exit:

#include stdio.hint main() {    int num1, reverse  0, rem;    char ch;    startp:    printf(Enter a number: );    scanf(%d, num1);    reverse  0;    while (num1 ! 0) {        rem  num1 % 10; // Get the last digit        reverse  reverse * 10   rem; // Build the reversed number        num1 / 10; // Remove the last digit from num1    }    printf(Reversed Number: %d
, reverse);    again:    printf(Do you want to reverse another number (Y/N)? );    scanf(%c, ch);    if (ch  'y' || ch  'Y') {        goto startp;    } else if (ch  'n' || ch  'N') {        return 0;    } else {        printf(Invalid input. Please enter Y or N.
);        goto again;    }    return 0;}

Explanation of the Code

Input Validation

The program first checks if the input number is a 4-digit number between 1000 and 9999. If not, it prompts the user to enter a valid number and exits.

The looping mechanism is used to extract each digit from the number.

The last digit is obtained using the modulus operator `% 10`.

This digit is added to the `reverse` variable, and the `reverse` variable is multiplied by 10 to shift its digits left.

The last digit is then removed from `num1` using integer division `num1 / 10`.

Output

Finally, the program prints the reversed number.

Compilation and Execution

Compile and run this program using any standard C compiler. Ensure to provide a valid 4-digit number when prompted.

Example Output

Enter a number: 3456Reversed Number: 6543Do you want to reverse another number (Y/N)? yEnter a number: 9402Reversed Number: 2049Do you want to reverse another number (Y/N)? n

Conclusion

This guide covers the basic structure and functionality of reversing a 4-digit number in C programming. By following the steps and logic presented, you can create a robust and flexible program that meets your needs.