Print all palindromes in a given range

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


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







© 2021, kalkicode.com, All rights reserved