GoogleTag

Google Search

[Programming] Question 24: Check whether a number is a palindrome

 In programming, understanding how to determine if a number is a palindrome is a valuable skill. A palindrome is a number that reads the same forward and backward, such as 121 or 1331. This question challenges you to develop an efficient algorithm that checks for this property, enhancing your problem-solving abilities and understanding of string manipulation.

#include <stdio.h>

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

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

	originalNumber = num;

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

	if (originalNumber == reversedNumber)
		printf("%d is a palindrome.\n", originalNumber);
	else
		printf("%d is not a palindrome.\n", originalNumber);

	return 0;
} 

Inside the main() function:

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

- The user is prompted to enter an integer.

- The original number is stored in `originalNumber`.

- 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.

- If the original number is equal to the reversed number, it is a palindrome; otherwise, it is not.

- The program prints the result accordingly.



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