1. Write a flowchart to input a whole number and output “It’s PERFECT” if the input is a “perfect number”; otherwise, it should output “It’s IMperfect”. A “perfect number” is a positive whole number that is equal to the sum of all its positive divisors except itself. For example, 12 is imperfect because its divisors 1, 2, 3, 4, 6 do not sum to 12. As another example, 28 is perfect because its divisors 1, 2, 4, 7, and 14 sum to 28.
2. Write a flowchart to input 40 numbers and output “yes” if number of negative values among the first 10 inputs is equal to number of positive values among the last 10 inputs. Note. The 20 numbers in middle are ignored. For instance, if the values are: 1,-1,1,1,-1,1,-2,5,1,1,4,4,5,-6,5,8,-12,0,2,0,1,2,3,7,7,-7,0,1,1,1,3,4,1,-1,-1,-1,-1,-1,-1,-1
the program should output “yes” since the number of negative values in the first 10 numbers is 3, and the number of positive values in the last 10 numbers is 3 too. As another example, if the values are: 1,0,1,0,1,2,1,0,-1,-1,0,0,0,1,1,1,-5,-6,0,0,1,-12,-12,-1,-1,1,1,1,1,1,1,0,-1,0,1,-2,1,2,1,1
the program should output “no” because number of negative values in the first 10 numbers (1,0,1,0,1,2,1,0,-1,-1,) is 2 but number of positive values in the last 10 numbers (1,0,-1,0,1,- 2,1,2,1,1) is 6. Note that we do not count zeros neither as negative nor as positive.
3. Write a flowchart to input numbers until 0 is entered and then output the maximum number of consecutive 1s that were entered. For example, if the entered values are 1,2,4,5,1,1,3,4,1,1,1,8,6,1,1,3,2,0, the program should output 3 since the maximum number of consecutive 1s is 3. As another example, if the entered values are 2,3,5,9,2,3,3,3,0, the program should output 0 since there is no 1 in the input. As a 3rd example, if the entered values are 4,9,1,5,6,8,2,1,3,3,3,3,0, the program should output 1 since the maximum number of consecutive 1s is 1.
4. Write a flowchart to input a whole number and output “This number is self-dividing” if the input is a “self-dividing number”; otherwise, it should output “This number is NOT self- dividing”. A “self-dividing number” is a number that is divisible by every digit it contains. Note that in addition, a self-dividing number is not allowed to contain the digit zero. For example, 128 is a self-dividing number because 1, 2, and 8 are all divisors of 128. As another example, 102 is not a self-diving number because it contains a digit 0. As a 3rd example, 26 is not a self-dividing number, because it’s not divisible by 6.
5. Devise an algorithm to first input n, as the number of digits of a binary number. Then, it should input all digits of that binary number and convert the number to decimal. For example, if 5 is entered for n, the program should receive 5 binary digits, let’s say they are 1, 1, 0, 0, 1
then the program should output 25 because 1*24 + 1*23 + 0*22 + 0*21 + 1*20 =25.
In general, if a binary number has n digits as d1d2d3…dn-1dn , it can be converted to decimal by this formula d1*2n-1 + d2*2n-2 + d3*2n-3 + … dn-1*21 + dn*20. As another example, if 8 is entered for n, and the 8 digits are 0, 0, 1, 0, 0, 1, 1, 1 then the program should output 39 because 0*27 + 0*26 +1*25 +0*24 +0*23 +1*22 +1*21 +1*20 =39.
6. Write a flowchart to input a whole number, k, and output “it’s balanced” if sum of the digits in the first half of k is equal to sum of the digits in the 2nd half; otherwise, it should output “it’s NOT balanced”. Example 1. If the input is 13522, the program should output “It’s balanced”. Note that sum of the digits in the first half (i.e. 1 and 3) is 4 and that’s equal to sum of the digits in the second half (i.e. 2 and 2). Example 2. if the input is 4893, the program should output “It’s balanced”, as sum of the digits in the first half (i.e. 4 and 8) is 12 and that’s equal to sum of the digits in the second half (i.e. 9 and 3). Example 3. If the input is 352, the program should output “It’s NOT balanced” because sum of digits in the first half is 3 whereas sum of the digits in the second half is 2.
7. Write a flowchart to calculate an approximate value for the square root of given input m. You are NOT allowed to use the sqrt function or √𝑚 or m0.5. Instead, you are required to use the following approach. The square root of a number m can be calculated approximately as
𝑠𝑛𝑒𝑥𝑡 = 1 (𝑠𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 + 𝑚 ) . 𝑠𝑛𝑒𝑥𝑡 is a better approximation than 𝑠𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 and the more 2 𝑠𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠
steps in the process the better approximation 𝑠𝑛𝑒𝑥𝑡 becomes. One should stop this process once the difference between 𝑠𝑛𝑒𝑥𝑡 and 𝑠𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 is less than some small value, say 0.000001.
The first value of 𝑠𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 can be chosen as 𝑚 perhaps. As an example, the square root of 9 2
is approximated as follows. The first value for s is 9 or 4.5 and we should keep applying the 2
formula above to calculate new values of s. So, all values of s would be: 4.5, 3.25, 3.009615384615, 3.000015360039, 3.000000000039, 3.000000000000 As the difference of the last two values of s is less than 0.000001, the program stops and reports the last value, i.e. 3, as the square root of 9.
1. Sol:
2. Sol:
Pre: num >0 N
Post: determines whether num is perfect or not.
Pre: num R
Post: outputs “yes” if number of negative values in the first 10 is the same as number of positive values in the last 10.
3. Sol:
Pre: num R
Post: outputs maximum number of consecutive 1s.
4. Sol:
Pre: num N
Post: outputs “yes” if num is self-dividing.
5. Sol:
Pre: n N, d {0,1}
Post: outputs the decimal representation of the sequence of d’s.
6. Sol:
Pre: k N
Post: outputs “it’s balanced” if k is balanced by definition; otherwise, outputs “it’s NOT balanced”.
7. Sol:
Pre: m N
Post: outputs an approximation for square root of m.