1. Does the code look correct?
2. Does the code compile?
3. During testing, does it give the expected results? Remember to test for all possible input conditions. (not every possible number, just samples of values which meet the various conditions).
My responses.
1. The code looks erroneous in a few places, as well as some suspicious-looking use of semicolons.
2. Yes, the code does compile.
That is useful, because it means you can then go on to test it.
The program should display the following menu:
1) number divisible by 3
2) number between 10 and 100 |
In fact it displays
That's the first problem - the menu does not appear as specified. How is the user supposed to know what 1 and 2 mean?
Next, let's test option 1.
if the user enters the value 1, determine whether the vale 3 is a divisor of the integer number and output an appropriate message |
The program fails to display anything at all.
Now let's test option 2.
determine whether the integer number is less than 10, between 10 and 100, or greater than 100 and output an appropriate message |
No matter what input is given, the output is always:
Less than 10 :
Greater than 100 : |
Is this correct? There is one part which seems correct. If the user enters a value which is not 1 or 2, it does indeed display a message to inform the user.
Some of the code appears to be heading in the right direction, but needs work.
Line 14 - starting a line with a semicolon is not good practice. Usually place semicolons (where needed) at the
end of a line, not the start.
Line 43
else if(a > 10 && a > 100)
looks pretty odd. If a number is greater than 100 it must be greater than 10, so that test seems unnecessary.
Line 47 looks wrong.
else(a > 100)
The compiler may swallow it without a murmur, but it is definitely incorrect.
The
else
should stand alone (it cannot have an attached condition).
If another condition is necessary, then use another
if
. (Sometimes the else and if may be placed on the same line, so it looks like this:
else if (a > 100)
but remember the condition belongs to the if. Also, remember if you do add an
if
then it will also usually need its own corresponding
else
later.
Line 48. A stray semicolon at the start of the line. I guess this was added to patch up the error on the previous line. Remove the semicolon.
Regarding the logic for this part.
The variable name 'a' is meaningless. If you gave it a more meaningful name such as 'menu' or 'choice' it might help you to see where some of the mistakes are in the code.
Now, please don't respond with more code and "Is this correct?" Do some testing - see whether or not it works for yourself. If you do get stuck, then please do ask again if necessary. But don't depend on others to do the testing for you, that's part of your job as the programmer.