This assignment is based on a simple implementation task used during hiring interviews. It tests basic skills in programming and arithmetics.
- Atom or any other editor
- gcc
- terminal.
- The program asks the user for two pieces of information (repeat asking until proper values have been supplied):
- If an Add (1) or Multiply (2) operation should be performed
- To select a number in the range of 1-100
-
Based on the information from the user you will iterate over the range starting with 1 and ending with the number the user specified.
-
For each of the numbers you iterate over calculate if it is a multiple of 3 or 5. Ignore those numbers which don't fall into this category.
-
Based on the operation the user selected add or multiply the numbers you just filtered in the previous task. Be careful to detect an overflow (especially when multiplying) and in this case print an appropriate message and stop the calculation. You may use a long to allow for bigger results.
-
Print the result of the operation (except if an overflow occured).
-
Be careful to break down the task into separate functions (usually one function for each clearly separate task).
Take a look at these sample program runs. They show which results your program should calculate and how it should handle invalid input and overflows.
me@dev:~/if-03-22-A$ ./CondArith
Select Add (1) or Multiply (2): 3
Select Add (1) or Multiply (2): 1
Select a number in the range (1-100): 321
Select a number in the range (1-100): 20
The result is: 98
me@dev:~/if-03-22-A$ ./CondArith
Select Add (1) or Multiply (2): 2
Select a number in the range (1-100): 0
Select a number in the range (1-100): 10
The result is: 8100
me@dev:~/if-03-22-A$ ./CondArith
Select Add (1) or Multiply (2): 2
Select a number in the range (1-100): 50
Overflow!
- Take care to keep the work loop "Implement a little", "Test a little" to avoid the 100 lines of error mess.
All coding assignments will get checked. Most common reasons that your assignment is marked down are:
- Program does not build or builds with warnings
- One or more items in the Required Tasks section are not satisfied
- Submitted code is visually sloppy and hard to read
- Repeat using primitive data types, like integer and long
- Repeat implementing functions
- Repeat using conditions
- Work with a simple loop