CSI 205-Exam II
CSI 205-Exam II
Total
Question 1 2 3
Grade
Weight 35% 35% 30% 100%
Learning Outcomes 4,5
Grade
[35 points] Problem 1
Write a function called getDigit() that returns the nth digit of an integer m. In the main, the user enters
the integer m and specifies also which digit in m he/she would like to print. You should then call the
function getDigit() and pass to it m and n so that it gives you back the corresponding digit. Your
function should return -1 in case the user chooses to print a digit that is outside the boundaries of the
m.
Note that the digits in m are numbered from left to right i.e. the leftmost digit is the first, the one on its
right is the second, and so on.
Example:
- If m = 2020 and n = 3 then your program should print 2 (since the rightmost 2 is the third
digit).
- If m = 2020 and n = 5 then your program should print -1 (since the value 2020 is composed of
4 digits only).
- If m = 2345012714 and n = 6 then the program prints 1 (the leftmost 1).
Example:
If n = 5, your program prints the following:
-*
*-*-
-*-*-*
*-*-*-*-
-*-*-*-*-
Your program should read n from the user in the main and then call the function to get the above shape.
If there is no mathematical relation between n and m, your program should print “No relation exists!”
e.g.
- If n = 20 and m = 6 then your function prints “No relation exists!”.
Read both integers from the user in the main and then call these two functions and print the result
accordingly.
Note: you may NOT use any built-in functions in this exercise.
Page 2 of 2