Академический Документы
Профессиональный Документы
Культура Документы
7. Implement in C++ the following PDL program (partially developed for Homework #6), that reads
in an integer value representing somebody's year of birth (expressed in the YYYY format) and
produces that person's "magic lucky number". Such a number is obtained by repetitively adding
up all the digits in that year until they reduce to a single digit.
For example, the magic number for 1982 is 2 (first 1 + 9 + 8 + 2 = 20, then 2 + 0 = 2).
magicYourYear
call getBirthYear //read in a valid year number
call produceMagicNumber //calculate the magic number
output( "You were born in : ", birthYear)
output( "The magic number is: ", magicNumber) //output magic number
//It reads in a number representing a valid year of birth (i.e. positive or zero).
//If the user enters an invalid value the program displays an error message
//and ask for another input until a valid value is read in
//pre-condition: none
//post-condition: produces birthYear
proc getBirthYear
output( "Enter the (four-digit) number representing a year: ")
input( birthYear)
while ( birthYear < 0) do
output( "ERROR: negative value!")
output( "Enter the (four-digit) number representing a year: ")
input( birthYear)
endwhile
endproc
You do NOT have to check the validity of the type of the input (i.e. you can assume that the user
will only enter a number for the year).