Recent Post

Example 11: Finding If A Number Is A Palindrome

#!/usr/bin/tclsh

puts "Enter a number: "
gets stdin input

proc palindrome {input} {
	set len [ string length $input ]
	set n [ expr $len/2 ]

	for { set i 0 } { $i < $n } { incr i 1 } {
		set b [ string index $input $i ]
		set c [ expr $len - 1 - $i ]
		set d [ string index $input $c ]
	}

	if {$b == $d} {
		puts "This number is a palindrome"
	} else {
		puts "This number is not a palindrome"
	}
}

palindrome $input
Output:
Enter a number:
1001
This number is a palindrome
Enter a number:
123
This number is not a palindrome

Comments