How to reverse a number in C?
How to reverse a number in C?
Reversing the order of the digits of an integer is a fundamental programming operation known as "reversing a number". We can do this operation easily in C. It is mainly asked in numerous coding challenges, programming competitions, and real-world applications. One may reverse a number with loops and simple arithmetic operations.
The most typical way to reverse a number in C is to use a loop to take each digit out of the original number at a time.
The fundamental procedures for reversing a number are as follows:
- Enter the Number: The printf prompts the user to enter an integer number, and scanf reads the input.
- Initialize Variables: The initial value of a variable, let's say reversed, is 0, which will hold the reversed value.
- Reverse the Number: To reverse the Number, we must first separate each digit. The modulo operator (%) and division operations (/) can do this. The rightmost digit of the number can be obtained by taking the remainder after dividing it by 10 (num% 10).
- Reverse the Digits: After removing the rightmost digit, we multiply the reversed variable by ten and add the removed digit to update it. This puts the new digit in the place of the one in the reversed number and effectively moves the current ones to the left.
- Remove the Last Digit: After adding the digit to the number turned around, we take the final digit out by dividing the number by 10 (num /= 10).
- Continue till the Number Becomes 0: We keep doing this until the starting number turns 0. The loop continues until all of the original number's digits have been handled.
- Publish the outcome: Finally, we use printf to show the user the reversed number.
See the following C program to show how to reverse a number:
Example:
#include <stdio.h>
int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}
int main() {
int num, reversedNum;
printf("Enter an integer number: ");
scanf("%d", &num);
reversedNum = reverseNumber(num);
printf("Reversed number: %d\n", reversedNum);
return 0;}
Sample Input 1:
Enter an integer number: 123
Sample Output 1:
Reversed number: 321
Explanation:
1. The user enters '123' when the application asks them to enter an integer number.
2. The 'reverseNumber' function is called by the 'main' function with the input of '123'.
3. 'ReverseNumber' function internals:
- The initial value of the inverted variable is 0.
- When the loop begins, the num is set to '123'.
- The digit is calculated as '123 % 10' in the initial iteration, yielding a value of '3'.
- The updated 'reversed' is '0 * 10 + 3', which is '3'.
- The new value of num is '12', or '123' divided by '10'.
- The cycle is repeated until num equals 0.
4. The 'reverseNumber' function gives back '321' after the loop.
5. The variable 'reversedNum' in the 'main' function holds the value '321' returned by 'reverseNumber'.
6. The program then shows the reversed Number using 'printf', which produces the output Reversed Number: '321'.
Sample Input 2:
Enter an integer number: 9876
Sample Output 2:
Reversed Number: 6789
Explanation:
1. When the application asks for an integer number, the user enters '9876'.
2. With the number '9876' input, the 'main' function calls the 'reverseNumber' function.
3. 'ReverseNumber' function internals:
- The initial value of the inverted variable is 0.
- When the loop begins, the num is set to '9876'.
- The first iteration yields six since the digit is calculated as 9876% of 10.
- The updated reversed is '0 * 10 + 6', which is 6.
- The current value of num is '987 (9876/10)'.
- The cycle is repeated until num equals 0.
4. The 'reverseNumber' method returns '6789' at the end of the loop.
5. The variable 'reversedNum' in the 'main' function holds the value '6789' returned by 'reverseNumber.'
6. The software then uses printf to display the flipped Number, producing the Number reversed: '6789'.
Conclusion
In summary, reversing a number in C is a simple operation that entails taking the digits from the original Number and adding them in the opposite order to get the reversed Number. The assignment can be quickly completed using a loop and fundamental mathematical procedures. Understanding loops, arithmetic operations, and integer manipulation are aided by knowing how to reverse a number, a fundamental programming ability. This information can be used in various real-world situations and coding problems. With this expertise, programmers can take on more challenging issues and lay a solid basis for C programming advancement.