You are on page 1of 2

STIA1014 Assignment 2

(Due 18/11/2012)
For each question, you must submit a hardcopy and a softcopy of your source code (.java file). In the hardcopy, print some sample outputs from the execution of your programs.

1. Write an application that asks a user to type 1, 2, 3, or 4. When the user types 4, the program ends. When the user types 1, 2, or 3, the program displays the message Good job! and then asks for another input.When the user types anything else, the application issues an error message and then asks for another input. Save the file as
Asg21.java.

2. Write a program that reads in a positive integer N and then calculates and displays the sum of the first N odd integers. For example, if N is 4, your program should display the value 16, which is 1 + 3 + 5 + 7. Save the file as Asg22.java. 3. Write a program that reads a positive integer (> 0). Then, it adds all the digits in the integer and prints the result. For example, if an integer is 932, the sum of all its digit is 14. Save the file as Asg23.java. Hint: Use the % operator to extract digits and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.

4. Write a program that asks the user to input two integers. The program can then determine the greatest common divisor (GCD) of the two integers and prints it out. For example, if the two integers are 6 and 15, the GCD is 3.Save the file as
Asg24.java.

5. Write a program that asks the user to enter any number of positive integers and print out the biggest and the smallest numbers. Use a negative number as a sentinel to signal the end of the input. For example, if the inputs are: 3 5 4 2 10 3 7 -1

The ouput is: The biggest number is 10 The smallest number is 2

Save the file as Asg25.java. 6. Write an application that displays every perfect number from 1 through 1,000. A perfect number is one that equals the sum of all the numbers that divide evenly into it. For example, 6 is perfect because 1, 2, and 3 divide evenly into it, and their sum is 6; however, 12 is not a perfect number because 1, 2, 3, 4, and 6 divide evenly into it, and their sum is greater than 12. Save the file as Asg26.java.

You might also like