function getPositiveInteger : int
const ENTER : char := chr (10) % Constant ASCII code for the Enter Key
var ch : string (1) := 'x'
var number : int := 0
var digits : int := 0
put "Please enter a positive Integer : " ..
loop
if hasch then
getch (ch)
if ch >= '0' and ch <= '9' then
number := number * 10 + strint (ch)
digits := digits + 1
elsif ch not= ENTER then
digits := 0
number := 0
cls
put "** Invalid Entry Attempted**"
put
"Please enter an Integer greater than or equal to zero: "
..
end if
end if
exit when ch = chr (10) or digits >= 9
end loop
result number
end getPositiveInteger
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getProperFactors (n : int) : int
var x, y, temp : int := 0
for decreasing i : n .. 1 by 1
x := n mod i
if x = 0 then
y := n div i
if y not= n then
temp := temp + y
else
exit when i = 100
end if
end if
end for
result temp
end getProperFactors
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getDigitalRoot (var n : int) : int
var a, b, c, x : int := 0
loop
a := n mod 10
n := n div 10
c := c + a
exit when a = 0
end loop
a := c div 10
b := c mod 10
x := a + b
result x
end getDigitalRoot
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getReverse (n : int) : int
var reverse : int := 0
var r : int := n
loop
reverse := reverse * 10 + (r mod 10)
r := r div 10
exit when r = 0
end loop
result reverse
end getReverse
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getPrime (n : int) : int
for j : 1 .. 100 by 1
var y : int
y := getProperFactors (j)
if y = 1 then
put j : 3 ..
end if
end for
end getPrime
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
function palindrome (n : int) : boolean
if n = getReverse (n) then
result true
else
result false
end if
end palindrome
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkPrime (n : int) : boolean
for c : 2 .. (n - 1)
if (n mod c) = 0 then
result false
end if
end for
result true
end checkPrime
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getPerfect (n : int) : real
var sum : int := 0
for i : 1 .. round (n / 2)
if n mod i = 0 then
sum := sum + i
end if
end for
result n
end getPerfect
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkPerfect (n : int) : boolean
var sum : int
if n = getPerfect (sum) then
result true
else
result false
end if
end checkPerfect
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getHappy (n : int) : int
var a, b, d : int := 0
a := n mod 10
b := n div 10
d := a ** 2 + b ** 2
result d
end getHappy
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkHappy (n : int) : boolean
var x, y : int := 0
for increasing : n .. 15 by 1
if getHappy (n) < 15 then
result true
else
result false
end if
end for
end checkHappy
%~~~~~~~~~~~~~~~~~~Main Program~~~~~~~~~~~~~~~~~~~~~~~
var a, b, c : real := 0
var count : int := 0
var n : int := 0
put " Prime number are: ", a
loop
a := getPrime (n)
count := count + 1
exit when count = 100
end loop
put " Perfect number are: ", b
loop
b := getPerfect (n)
count := count + 1
exit when count = 10000
end loop
put " Happy number are: ", c
loop
c := getHappy (n)
count := count + 1
exit when count = 500
end loop
var num : int
put "Please enter a positive number: " ..
num := getPositiveInteger
var a1, b1, c1 : int
var a2, b2, c2 : boolean
a1 := getReverse (num)
put " The reverse of the number is: " ..
b1 := getProperFactors (num)
put " The proper factors of the number is: " ..
c1 := getDigitalRoot (num)
put " The digital root is: " ..
a2 := checkPrime (num)
put "Prime ", a2
b2 := checkPerfect (num)
put "Perfect ", b2
c2 := checkHappy (num)
put " Happy ", c2
|