Anonymous

Register for more FREE stuff!

my subscriptions

Loops

Question 1

[Maximum mark: 4]



Create a Java program which calculates the sum of the first three positive integers. Implement a for loop for this task and in the end print out the sum.


The program should print:

  • "The sum of the first three positive integers is {sum}."

Answers and Explanations

Question 2

[Maximum mark: 4]



Write a Java program which calculates the product of the first five positive integers. Implement a for loop for this task and in the end print out the sum.


The program should print:

  • "The product of the first five positive integers is {product}."

Answers and Explanations

Question 3

[Maximum mark: 5]



Write a Java program that prompts the user to enter a positive integer and then prints the multiplication table for that number up to 10. The program should use a loop to generate and display the table. The example table for number 7 would look as follows:


7 x 1 = 7

7 x 2 = 14

7 x 3 = 21

7 x 4 = 28

7 x 5 = 35

7 x 6 = 42

7 x 7 = 49

7 x 8 = 56

7 x 9 = 63

7 x 10 = 70

Answers and Explanations

Question 4

[Maximum mark: 5]



Write a Java program that prompts the user to enter a positive integer. The program should then calculate and print the sum of all even numbers from 1 to that integer using a loop.


If a negative number is initially given, the program should print:

  • "Please insert a positive integer".

If a positive number is given, the program should print:

  • "The sum of all even numbers from 1 to {number} is {sum}."

Answers and Explanations

Question 5

[Maximum mark: 4]



Write a Java program that prompts the user to enter a positive integer. The program should then use a loop to count down from that integer to 1, printing each number in a new line.


If a negative number is initially given, the program should print:

  • "Please insert a positive integer".

If a positive number is given, for example 5, the program should print:

  • "5 4 3 2 1"

Answers and Explanations

Question 6

[Maximum mark: 7]



Write a Java program that asks the user to input a positive integer between 1 and 500. The program should then determine if the number is a "perfect number." A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). If the number is perfect, the program should print that it is a perfect number; otherwise, it should print that it is not.


For example, the number 28 is a perfect number, as its divisors (excluding itself) are 1,2,4,7,14, and 1+2+4+7+14 = 28.


If the integer given is not between 1 and 500, the program should print:

  • "Please insert a valid integer".

If the given number was 28, for example, then your program should print:

  • "The number 28 is a perfect number"

Answers and Explanations

Question 7

[Maximum mark: 6]



Write a Java program that prompts the user to input a base number and an exponent. The program should then calculate and display the result of raising the base to the power of the exponent using a loop.


Your program should print:

  • "{base} raised to the power of {power} equals {result}".

Answers and Explanations

Question 8

[Maximum mark: 6]



Write a Java program that continuously prompts the user to input positive numbers and calculates their cumulative sum. The program should stop when the user inputs a negative number and print the total sum of the positive numbers entered.


  • Hint: use a while loop for this question.

Your program should print:

  • "The total sum of the positive numbers entered is {sum}"

Answers and Explanations

Question 9

[Maximum mark: 6]



Write a Java program that prompts the user to enter a series of integers. The program should use a while loop to count how many of the entered integers are even numbers. The loop should terminate when the user inputs 0. After exiting the loop, the program should print the count of even numbers.


Your program should print:

  • "The count of all even integers from this set of numbers is {sum}"

Answers and Explanations

Question 10

[Maximum mark: 7]



Write a Java program that repeatedly prompts the user to enter positive integers and calculates their sum. The program should use a while loop to continue asking for numbers until the user inputs a negative number. After the loop terminates, the program should print the average of all odd numbers.


Your program should print:

  • "The average of all odd integers from this set of numbers is {average}"

Answers and Explanations