Recent Post

Example 4: Calculate The Sum Of All The Digits Of A Number

#!/usr/bin/tclsh

puts "Enter a number: "
gets stdin number
set sum 0
scan $number %d number
while {$number > 0} {
	set sum [ expr $sum + $number % 10]
	set number [expr $number / 10]
}
puts "Sum of the digits of $number is: $sum"
Output:
Enter a number:
00124
Sum of the digits of 00124 is: 7
Enter a number:
7600102
Sum of the digits of 7600102 is: 16

Comments