Programs using IF
- Write a program to get a number from the user.
- Print whether the number is negative, positive or zero.
- Print whether the number is odd or even (I guess this only applies to integers)
- Print whether the number is a whole number (integer) or a decimal. (This might be tricky).
Write a program that does the following:
- Read in two integers from the user and call them 'a' and 'b'
- if either number is negative, print "green"
- if both numbers are negative, print "navy"
- only if 'a' is even then do the following
- if 'b' > 'a' print "crimson"
- otherwise print "pink"
A number could fall into more than one category, so you can get more than one colour printed out.
Can you think of a clever way of doing step #2?
Note: you will probably have to look up how to tell if a number is even or not (for step #4). If you cannot get this done, hand it in anyway for part marks
Here is some test data. If you type in the values below for a,b, then your program should be printing the colours show in the 'Result' column
'a' 'b': Result ====================== 2 4 : crimson, yellow 4 1 : pink, yellow 20 4 : pink 4 20 : crimson 4 100 : crimson, yellow 3 8 : -- nothing -- 100 4 : pink, yellow 51 -1 : green -5 -4 : green, navy, yellow -4 -5 : green, navy, pink, yellow -4 -3 : green, navy, crimson, yellow -1 -1 : green, navy, yellow 1 -1 : green, yellow -4 38 : green, crimson -4 8 : green, crimson, yellow
(Let me know if you spot any errors! Thanks Nico and Aiden.)
NOTE: your program is not going to print out this table. What this table is for is to show that if your numbers are 4,1 then it should print out pink,yellow
This program is to examine the importance of the order of If statements
- Get a number from the user
- If the number is greater than 5 print "yes",
- unless it is greater than 10 in which case print "never"
- Otherwise print "maybe"
We only want 1 word to be printed out!
SKIP THIS. It's just an algorithm
Write a program that gets two numbers from the user.
Check to see if the numbers are within 10 of each other
If the numbers are within 10, print "yes".
If not, print "no".
5, 7 yes 7, 5 yes 122, 136 no 136, 122 no -30, -36 yes -3, +3 yes
Quadratic.java Oct 2017: Done in class
Do this if you want to get more than 80% in the course.
Write a program to calculate the quadratic formula:
- The user will enter three numbers a, b, and c. (these variables should be of the type double)
Your program will check to make sure that they are all numbers (no words...) If one of them is not a number, pop up a message box, and then Exit Sub.
- Next you need to calculate the part under the square root: b*b - 4ac (store it in a variable; what will you call it?)
- If this part (technically called the discriminant) is negative, then you obviously can't take the square root. So the result you must display is "No roots"
- If this part is equal to zero, then there is only one root. Do the calculation to find it. Display the root.
- If this part is greater than zero, then there are two roots. Calculate each one, first using the "+" sign, and then again using the "-" sign. Display both roots.
Test with this data: a=1, b=2, c=3.6 No roots a=4, b=1, c= -3 x1 = 0.75 x2 = -1 a=3, b= -2, c=0 x1 = 0.6667 x2 = 0 a=1, b=2, c=1 one root x = -1