Print all palindromes in a given range
The problem is to find and print all palindrome numbers within a given range. A palindrome number is a number that remains the same when its digits are reversed. For example, 121 and 454 are palindrome numbers because their digits remain the same when reversed.
Problem Statement and Example
Given a range [s, e] (inclusive), the task is to find and print all palindrome numbers in that range. For instance, if the range is [50, 300], the output should be:
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Similarly, for the range [1000, 1200], the output should be:
Palindromes in range (1000 1200) is
1001 1111
Pseudocode
function reverse(number):
result = 0
while number > 0:
result = (result * 10) + (number % 10)
number = number / 10
return result
function palindromesInRange(s, e):
if s < 0 or e < 0:
return
count = 0
print "Palindromes in range (" + s + " " + e + ") is"
for i from s to e - 1:
if reverse(i) == i:
count = count + 1
print i
if count % 3 == 0:
print new line
if count == 0:
print "None"
main:
palindromesInRange(50, 300)
palindromesInRange(1000, 1200)
Algorithm Explanation
-
The
reverse
function takes an integernumber
as input and returns its reversal view. It initializes a variableresult
to 0. Then, it iterates through each digit of thenumber
using a while loop and appends each digit toresult
in a reversed manner. -
The
palindromesInRange
function takes two integerss
ande
as input, representing the lower and upper bounds of the range, respectively. It first checks if eithers
ore
is negative, in which case it returns immediately as the input range is invalid. -
It initializes a variable
count
to 0, which will be used to track the number of palindrome numbers found within the range. -
The function then prints the header with the given range.
-
It iterates through each number
i
in the given range froms
toe-1
. -
For each number
i
, it checks ifreverse(i) == i
. If this condition is true, it meansi
is a palindrome number, and it is printed. -
The
count
variable is incremented, and if the count becomes a multiple of 3, a new line is added to format the output. -
After the loop, if no palindrome numbers were found (
count == 0
), it prints "None."
Code Solution
Here given code implementation process.
/*
C program for
Print all palindromes in a given range
*/
#include <stdio.h>
// Returns the Reversal view of given number
int reverse(int number)
{
int result = 0;
while (number > 0)
{
// Append digit
result = (result *10) + (number % 10);
//remove last digit
number /= 10;
}
return result;
}
// Handles the request of display all palindrome in given range
void palindromesInRange(int s, int e)
{
if (s < 0 || e < 0)
{
return;
}
int count = 0;
printf("\n Palindromes in range (%d %d) is \n", s, e);
// Execute loop in given range
for (int i = s; i < e; ++i)
{
if (reverse(i) == i)
{
count++;
printf(" %d", i);
if (count % 3 == 0)
{
printf("\n");
}
}
}
if (count == 0)
{
printf("\n None");
}
}
int main(int argc, char
const *argv[])
{
// Test
palindromesInRange(50, 300);
palindromesInRange(1000, 1200);
return 0;
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
/*
Java program for
Print all palindromes in a given range
*/
public class Palindrome
{
// Returns the Reversal view of given number
public int reverse(int number)
{
int result = 0;
while (number > 0)
{
// Append digit
result = (result * 10) + (number % 10);
//remove last digit
number /= 10;
}
return result;
}
// Handles the request of display all palindrome in given range
public void palindromesInRange(int s, int e)
{
if (s < 0 || e < 0)
{
return;
}
int count = 0;
System.out.print("\n Palindromes in range (" + s + " " + e + ") is \n");
// Execute loop in given range
for (int i = s; i < e; ++i)
{
if (reverse(i) == i)
{
count++;
System.out.print(" " + i );
if (count % 3 == 0)
{
System.out.print("\n");
}
}
}
if (count == 0)
{
System.out.print("\n None");
}
}
public static void main(String[] args)
{
Palindrome task = new Palindrome();
// Test
task.palindromesInRange(50, 300);
task.palindromesInRange(1000, 1200);
}
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Print all palindromes in a given range
*/
class Palindrome
{
public:
// Returns the Reversal view of given number
int reverse(int number)
{
int result = 0;
while (number > 0)
{
// Append digit
result = (result *10) + (number % 10);
//remove last digit
number /= 10;
}
return result;
}
// Handles the request of display all palindrome in given range
void palindromesInRange(int s, int e)
{
if (s < 0 || e < 0)
{
return;
}
int count = 0;
cout << "\n Palindromes in range (" << s << " " << e << ") is \n";
// Execute loop in given range
for (int i = s; i < e; ++i)
{
if (this->reverse(i) == i)
{
count++;
cout << " " << i;
if (count % 3 == 0)
{
cout << "\n";
}
}
}
if (count == 0)
{
cout << "\n None";
}
}
};
int main()
{
Palindrome *task = new Palindrome();
// Test
task->palindromesInRange(50, 300);
task->palindromesInRange(1000, 1200);
return 0;
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
// Include namespace system
using System;
/*
Csharp program for
Print all palindromes in a given range
*/
public class Palindrome
{
// Returns the Reversal view of given number
public int reverse(int number)
{
int result = 0;
while (number > 0)
{
// Append digit
result = (result * 10) + (number % 10);
//remove last digit
number /= 10;
}
return result;
}
// Handles the request of display all palindrome in given range
public void palindromesInRange(int s, int e)
{
if (s < 0 || e < 0)
{
return;
}
int count = 0;
Console.Write("\n Palindromes in range (" + s + " " + e + ") is \n");
// Execute loop in given range
for (int i = s; i < e; ++i)
{
if (this.reverse(i) == i)
{
count++;
Console.Write(" " + i);
if (count % 3 == 0)
{
Console.Write("\n");
}
}
}
if (count == 0)
{
Console.Write("\n None");
}
}
public static void Main(String[] args)
{
Palindrome task = new Palindrome();
// Test
task.palindromesInRange(50, 300);
task.palindromesInRange(1000, 1200);
}
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
package main
import "fmt"
/*
Go program for
Print all palindromes in a given range
*/
// Returns the Reversal view of given number
func reverse(number int) int {
var result int = 0
for (number > 0) {
// Append digit
result = (result * 10) + (number % 10)
//remove last digit
number = number / 10
}
return result
}
// Handles the request of display all palindrome in given range
func palindromesInRange(s, e int) {
if s < 0 || e < 0 {
return
}
var count int = 0
fmt.Print("\n Palindromes in range (", s, " ", e, ") is \n")
// Execute loop in given range
for i := s ; i < e ; i++ {
if reverse(i) == i {
count++
fmt.Print(" ", i)
if count % 3 == 0 {
fmt.Print("\n")
}
}
}
if count == 0 {
fmt.Print("\n None")
}
}
func main() {
// Test
palindromesInRange(50, 300)
palindromesInRange(1000, 1200)
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
<?php
/*
Php program for
Print all palindromes in a given range
*/
class Palindrome
{
// Returns the Reversal view of given number
public function reverse($number)
{
$result = 0;
while ($number > 0)
{
// Append digit
$result = ($result * 10) + ($number % 10);
//remove last digit
$number = (int)($number / 10);
}
return $result;
}
// Handles the request of display all palindrome in given range
public function palindromesInRange($s, $e)
{
if ($s < 0 || $e < 0)
{
return;
}
$count = 0;
echo("\n Palindromes in range (".$s." ".$e.") is \n");
// Execute loop in given range
for ($i = $s; $i < $e; ++$i)
{
if ($this->reverse($i) == $i)
{
$count++;
echo(" ".$i);
if ($count % 3 == 0)
{
echo("\n");
}
}
}
if ($count == 0)
{
echo("\n None");
}
}
}
function main()
{
$task = new Palindrome();
// Test
$task->palindromesInRange(50, 300);
$task->palindromesInRange(1000, 1200);
}
main();
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
/*
Node JS program for
Print all palindromes in a given range
*/
class Palindrome
{
// Returns the Reversal view of given number
reverse(number)
{
var result = 0;
while (number > 0)
{
// Append digit
result = (result * 10) + (number % 10);
//remove last digit
number = parseInt(number / 10);
}
return result;
}
// Handles the request of display all palindrome in given range
palindromesInRange(s, e)
{
if (s < 0 || e < 0)
{
return;
}
var count = 0;
process.stdout.write("\n Palindromes in range (" +
s + " " + e + ") is \n");
// Execute loop in given range
for (var i = s; i < e; ++i)
{
if (this.reverse(i) == i)
{
count++;
process.stdout.write(" " + i);
if (count % 3 == 0)
{
process.stdout.write("\n");
}
}
}
if (count == 0)
{
process.stdout.write("\n None");
}
}
}
function main()
{
var task = new Palindrome();
// Test
task.palindromesInRange(50, 300);
task.palindromesInRange(1000, 1200);
}
main();
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
# Python 3 program for
# Print all palindromes in a given range
class Palindrome :
# Returns the Reversal view of given number
def reverse(self, number) :
result = 0
while (number > 0) :
# Append digit
result = (result * 10) + (number % 10)
# remove last digit
number = int(number / 10)
return result
# Handles the request of display all palindrome in given range
def palindromesInRange(self, s, e) :
if (s < 0 or e < 0) :
return
count = 0
print("\n Palindromes in range (", s ," ", e ,") is ")
i = s
# Execute loop in given range
while (i < e) :
if (self.reverse(i) == i) :
count += 1
print(" ", i, end = "")
if (count % 3 == 0) :
print(end = "\n")
i += 1
if (count == 0) :
print("\n None", end = "")
def main() :
task = Palindrome()
# Test
task.palindromesInRange(50, 300)
task.palindromesInRange(1000, 1200)
if __name__ == "__main__": main()
Output
Palindromes in range ( 50 300 ) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range ( 1000 1200 ) is
1001 1111
# Ruby program for
# Print all palindromes in a given range
class Palindrome
# Returns the Reversal view of given number
def reverse(number)
result = 0
while (number > 0)
# Append digit
result = (result * 10) + (number % 10)
# remove last digit
number = number / 10
end
return result
end
# Handles the request of display all palindrome in given range
def palindromesInRange(s, e)
if (s < 0 || e < 0)
return
end
count = 0
print("\n Palindromes in range (", s ," ", e ,") is \n")
i = s
# Execute loop in given range
while (i < e)
if (self.reverse(i) == i)
count += 1
print(" ", i)
if (count % 3 == 0)
print("\n")
end
end
i += 1
end
if (count == 0)
print("\n None")
end
end
end
def main()
task = Palindrome.new()
# Test
task.palindromesInRange(50, 300)
task.palindromesInRange(1000, 1200)
end
main()
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
/*
Scala program for
Print all palindromes in a given range
*/
class Palindrome()
{
// Returns the Reversal view of given number
def reverse(num: Int): Int = {
var result: Int = 0;
var number = num;
while (number > 0)
{
// Append digit
result = (result * 10) + (number % 10);
//remove last digit
number = number / 10;
}
return result;
}
// Handles the request of display all palindrome in given range
def palindromesInRange(s: Int, e: Int): Unit = {
if (s < 0 || e < 0)
{
return;
}
var count: Int = 0;
print("\n Palindromes in range (" + s + " " + e + ") is \n");
var i: Int = s;
// Execute loop in given range
while (i < e)
{
if (reverse(i) == i)
{
count += 1;
print(" " + i);
if (count % 3 == 0)
{
print("\n");
}
}
i += 1;
}
if (count == 0)
{
print("\n None");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Palindrome = new Palindrome();
// Test
task.palindromesInRange(50, 300);
task.palindromesInRange(1000, 1200);
}
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
/*
Swift 4 program for
Print all palindromes in a given range
*/
class Palindrome
{
// Returns the Reversal view of given number
func reverse(_ num: Int) -> Int
{
var result: Int = 0;
var number: Int = num;
while (number > 0)
{
// Append digit
result = (result * 10) + (number % 10);
//remove last digit
number = number / 10;
}
return result;
}
// Handles the request of display all palindrome in given range
func palindromesInRange(_ s: Int, _ e: Int)
{
if (s < 0 || e < 0)
{
return;
}
var count: Int = 0;
print("\n Palindromes in range (", s ," ", e ,") is ");
var i: Int = s;
// Execute loop in given range
while (i < e)
{
if (self.reverse(i) == i)
{
count += 1;
print(" ", i, terminator: "");
if (count % 3 == 0)
{
print(terminator: "\n");
}
}
i += 1;
}
if (count == 0)
{
print("\n None", terminator: "");
}
}
}
func main()
{
let task: Palindrome = Palindrome();
// Test
task.palindromesInRange(50, 300);
task.palindromesInRange(1000, 1200);
}
main();
Output
Palindromes in range ( 50 300 ) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range ( 1000 1200 ) is
1001 1111
/*
Kotlin program for
Print all palindromes in a given range
*/
class Palindrome
{
// Returns the Reversal view of given number
fun reverse(num: Int): Int
{
var result: Int = 0;
var number: Int = num;
while (number > 0)
{
// Append digit
result = (result * 10) + (number % 10);
//remove last digit
number = number / 10;
}
return result;
}
// Handles the request of display all palindrome in given range
fun palindromesInRange(s: Int, e: Int): Unit
{
if (s < 0 || e < 0)
{
return;
}
var count: Int = 0;
print("\n Palindromes in range (" + s + " " + e + ") is \n");
var i: Int = s;
// Execute loop in given range
while (i < e)
{
if (this.reverse(i) == i)
{
count += 1;
print(" " + i);
if (count % 3 == 0)
{
print("\n");
}
}
i += 1;
}
if (count == 0)
{
print("\n None");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Palindrome = Palindrome();
// Test
task.palindromesInRange(50, 300);
task.palindromesInRange(1000, 1200);
}
Output
Palindromes in range (50 300) is
55 66 77
88 99 101
111 121 131
141 151 161
171 181 191
202 212 222
232 242 252
262 272 282
292
Palindromes in range (1000 1200) is
1001 1111
Resultant Output Explanation
The provided code executes the main
function, which calls palindromesInRange
twice with
different ranges: palindromesInRange(50, 300)
and palindromesInRange(1000, 1200)
.
-
palindromesInRange(50, 300)
:- It finds and prints all palindrome numbers in the range [50, 300].
- The output shows palindrome numbers in groups of three, separated by spaces. If there are fewer than three numbers in a group, the group ends early.
- The output for this range is displayed as shown in the provided example.
-
palindromesInRange(1000, 1200)
:- It finds and prints all palindrome numbers in the range [1000, 1200].
- The output displays the palindrome numbers found in the given range.
Time Complexity
Let's analyze the time complexity of the code. For a given range [s, e], the palindromesInRange
function
iterates through all the numbers in the range, and for each number, it calls the reverse
function,
which itself iterates through the digits of the number. Therefore, the time complexity can be expressed as follows:
- For each number in the range [s, e], the
reverse
function runs in O(d) time, where d is the number of digits in the number. - Let n be the difference between e and s (e - s).
- The overall time complexity of the
palindromesInRange
function would be O(n * d), where d is the maximum number of digits in any number within the range.
As for the reverse
function, its time complexity is O(d), where d is the number of digits in the input
number.
Overall, the time complexity of the provided code is O(n * d), where n is the size of the range (e - s) and d is the maximum number of digits in any number within the range.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment