More Exercises
1. Write an application that declares three integer variables (a, b & c). Set a to 15 and b to 6. Add a & b and put the result into c. Print out the result in c with a label saying that it is the sum. Now take a mod b, and put the result into c. Print out c, with a label identifying it as c (“c = “).
2. Declare float variables to hold 17 and 3. Declare a string variable to hold the string “The answer is: “. Print out the result of dividing 17 by 3, labeled with the string (“The answer is 5.666”).
3. Declare and initialize variables for your first name, last name, middle initial, and age. (The contents of the last variable can be made up, if you want!) Using your variables, print out your name – last name first, comma, first name. Skip a line. Then print out your age on another line.
4. Print the line “This is a loop test” on the screen 3 times, on three separate lines.
5. Print the line “This is a loop test” on the screen 3 times, with a space between each line.
6. Print on the screen (using a loop, of course), this:
This is line 1.
This is line 2.
This is line 3.
7. Print on the screen these lines:
46. This is a line.
47. This is a line.
48. This is a line.
8. Using a conditional statement, check whether a number in a variable is even or odd. Print out either “even” or “odd”. Try several numbers in the variable, to be sure your program is working correctly. HINT: You can tell whether a number is even or odd by checking its modulus 2.
9. Declare an integer variable. Then use a conditional to do the following: If the variable is 2, print “It’s a 2.” If the variable is 3, print “It’s a 3.” Otherwise, print “It’s something else.” Now try putting 2, 3 and 5 into the variable. Did it work right for 2? (It should print only “It’s a 2”, and not “It’s something else.”) If not, it is because of the way you have nested your “if’s”.