C Online Compiler
Example: Palindrome Numbers in Range (String Conversion) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Palindrome Numbers in Range (String Conversion) #include <stdio.h> #include <string.h> // For strlen #include <stdlib.h> // For sprintf // Function to check if a number is a palindrome using string conversion int isPalindromeString(int n) { char str[20]; // Buffer to hold the string representation of the number // Handle negative numbers if (n < 0) { return 0; } // Convert integer to string sprintf(str, "%d", n); int length = strlen(str); int i = 0; // Pointer from the start int j = length - 1; // Pointer from the end // Compare characters from both ends towards the middle while (i < j) { if (str[i] != str[j]) { return 0; // Not a palindrome } i++; j--; } return 1; // It is a palindrome } int main() { int start_range, end_range; // Step 1: Get user input for the range printf("Enter the starting number of the range: "); scanf("%d", &start_range); printf("Enter the ending number of the range: "); scanf("%d", &end_range); // Step 2: Ensure valid range if (start_range > end_range) { int temp = start_range; start_range = end_range; end_range = temp; } // Step 3: Print header for results printf("Palindrome numbers in the range %d to %d are:\n", start_range, end_range); // Step 4: Iterate through the range and check each number for (int i = start_range; i <= end_range; i++) { if (isPalindromeString(i)) { printf("%d ", i); } } printf("\n"); return 0; }
Output
Clear
ADVERTISEMENTS