Site icon DataFlair

C Program to Reverse a Number

c program to reverse a number

Reversing a number is a common task in programming that involves reversing the order of its digits. In this article, we will explore how to write a C program to reverse a number, along with the code, results, and alternative approaches to achieve the same result.

Code to Reverse a Number in C:

#include <stdio.h>
int reverseNumber(int number) { int reversedNumber = 0;
while (number != 0) {
int remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
return reversedNumber;
}
int main() {
int number;
printf("Enter a number: "); scanf("%d", &number);
int reversedNumber = reverseNumber (number); 
printf("Reversed number: %d\n", reversedNumber); 
return 0;}

Explanation:

1. We define a function `reverseNumber()` that takes an integer as an input and returns the reversed number.

2. Inside the function, we initialize a variable `reversedNumber` to store the reversed number.

3. We use a `while` loop to iterate until the number becomes zero.

4. In each iteration, we find the remainder of the number when divided by 10 using the modulo operator `%`.

5. We then update the `reversedNumber` by multiplying it by 10 and adding the remainder.

6. We divide the number by 10 to remove the last digit in each iteration.

7. Finally, we return the `reversedNumber` once the loop is complete.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

8. In the `main()` function, we prompt the user to enter a number, read it using `scanf()`, and store it in the `number` variable.

9. We call the `reverseNumber()` function with `number` as the argument and store the result in `reversedNumber`.

10. We print the reversed number using `printf()`.

Alternate Approaches:

While the above code efficiently reverses a number, there are alternative approaches that can achieve the same result. Here are a few such methods:

Using an Array to reverse a Number in C:

– Convert the number into a string or character array.

– Reverse the order of characters in the array.

– Convert the reversed array back to an integer.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int reverseNumber(int number) { 
char dataFlair[20];
sprintf(dataFlair, "%d", number); 
int length = strlen(dataFlair);
for (int i = 0; i < length / 2; i++) 
{ char temp = dataFlair[i];
}
dataFlair[i] = dataFlair[length - i - 1]; dataFlair[length - i - 1] = temp;
return atoi(dataFlair);
}
int main() {
int number;
printf("Enter a number: "); scanf("%d", &number);
int reversedNumber = reverseNumber (number); 
printf("Reversed number: %d\n", reversedNumber); 
return 0;}

C Program to reverse a number Using Recursion:

– Divide the number by ten recursively until it becomes zero.

– In each recursive call, extract the last digit of the number.

– Concatenate the digits to form the reversed number.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int reverseNumber(int number) { char dataFlair[20];
printf(dataFlair, "%d", number); 
int length = strlen(dataFlair);
for (int i = 0; i < length / 2; i++) 
{ char temp = dataFlair[i];
}
dataFlair[i] = dataFlair[length - i - 1]; 
dataFlair[length - i - 1] = temp;
return atoi(dataFlair);
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int reversedNumber = reverseNumber (number); 
printf("Reversed number: %d\n", reversedNumber); return 0;
}

C Program to reverse a number Using Mathematical Operations:

– Calculate the number of digits in the given number.

– Extract the last digit using modulo and divide the number by 10.

– Multiply the extracted digit by the power of 10 based on its position.

– Add the extracted digit to the reversed number.

– Repeat the above steps until all digits are reversed.

#include <stdio.h> 
#include <math.h>
int reverseNumber(int number) { 
int reversedNumber = 0;
int numDigits = log10(number) + 1;
while (number != 0) {
int remainder = number % 10; 
reversedNumber += remainder * pow(10, numDigits - 1);
number /= 10;
numDigits--;
}
return reversedNumber;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int reversedNumber = reverseNumber (number); 
printf("Reversed number: %d\n", reversedNumber); 
return 0;
}

These alternative approaches provide different ways to reverse a number using arrays, recursion, and mathematical operations. But the end result is the same for all…

Output

Enter a number: 1234567
Reversed number: 7654321

Conclusion

Reversing a number is a fundamental programming task that can be approached using various techniques. The C program provided in this article demonstrates an efficient method to reverse a number using a loop. Additionally, we explored alternative approaches involving arrays, recursion, and mathematical operations. Choosing the right approach depends on the requirements and constraints of the specific scenario.

Exit mobile version