Skip to content

C Program to Reverse a Number

Category
Programming
On this page

In the world of programming, there are multiple problems that can be solved in various ways, depending on the chosen approach and the programming language used. One such problem is reversing a number, which is a common task given to beginners in the field of programming.

This article will present a step-by-step guide on how to write a program in C that reverses a given number. C is a general-purpose programming language that provides low-level access to memory and is crucial in the field of systems programming.

Problem Statement

The task is to create a C program that takes a number as input and then reverses that number.

For example, if the input is `12345`, the output should be `54321`.

Approach

To reverse a number, we will:

  • Initialize a variable reverse as 0 which will hold our reversed number.
  • Loop until the number becomes 0.
  • In each iteration, we will take the last digit of the number and add it to reverse after shifting its digits to the left (i.e., multiplying reverse by 10).
  • Finally, we will print the reversed number.

Algorithm

  1. Read the number to reverse.
  2. Initialize a variable reverse to `0`.
  3. Repeat steps 4-6 until the number is not equal to `0`.
  4. Get the last digit of the number using the modulus operator `%`.
  5. Add the last digit to reverse after shifting its digits to the left (i.e., reverse = reverse * 10 + lastDigit).
  6. Remove the last digit from the number using integer division by `10` (i.e., number = number / 10).
  7. Print the reversed number.

C Program to Reverse a Number

Here is a C program that implements the above approach and algorithm:

Explanation

In this program, the user is asked to enter an integer which is stored in the `number` variable. We then enter a while loop, which continues until `number` equals 0.

Inside the loop, we first calculate the remainder of `number` divided by 10. This gives us the last digit of number. We then multiply `reverse` by 10 (shifting reverse’s digits to the left) and add the remainder. This effectively adds the last digit of `number` to `reverse` .

Finally, we remove the last digit from `number` by dividing it by 10. This is possible because C uses integer division, which discards the remainder.

After the loop finishes, we print `reverse`, which now holds the `reverse` of the original input number.

Conclusion

The program to reverse a number in C demonstrates the application of basic arithmetic and control flow constructs. This exercise is a good practice for beginners in C programming to understand the working of loops, modulus operator, and integer division. The approach taken here is simple and efficient. However, one must ensure correct data type is used to prevent overflow for large numbers.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top