Input: num = 1234 Output: 4321 Input: num = 45910 Output: 1954
Algorithm to Reverse a Number.
- Extract the last digit of num using num % 10 and store it in a variable remainder.
- Append the remainder to the reversedNum by multiplying reversedNum by 10 and adding the remainder.
- Remove the last digit from num by dividing it by 10.
Program to Reverse an Integer.
//C program to reverse a number #include <stdio.h> int main() { int num, reversedNum = 0, remainder; printf("Enter an integer: "); scanf("%d", &num); while (num != 0) { // Extract the last digit remainder = num % 10; // Append the digit to the reversed number reversedNum = reversedNum * 10 + remainder; // Remove the last digit num /= 10; } printf("Reversed number: %d\n", reversedNum); return 0; }
Enter an integer: 1246
Reversed number: 6421
Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.