GoogleTag

Google Search

[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

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts