[Programming] Question 23: Write a Program to reverse a number

 

#include <stdio.h>

int main() {
	int num, reversedNumber = 0, remainder;

	printf("Enter an integer: ");
	scanf("%d", &num);

	while (num != 0) {
		remainder = num % 10;
		reversedNumber = reversedNumber * 10 + remainder;
		num /= 10;
	}

	printf("Reversed number = %d\n", reversedNumber);

	return 0;
} 

Inside the main() function:

- Integer variables `num`, `reversedNumber`, and `remainder` are declared to store the input number, reversed number, and remainders respectively.

- The user is prompted to enter an integer.

- Using a while loop, the number is reversed digit by digit by extracting each digit using the modulus operator and adding it to the reversedNumber after multiplying it by 10.

- Finally, the reversed number is printed to the console.



Featured Posts

Leetcode 4. Median of Two Sorted Arrays

  4. Median of Two Sorted Arrays Hard Given two sorted arrays  nums1  and  nums2  of size  m  and  n  respectively, return  the median  of t...

Popular Posts