The sum of the first three positive integers is 6.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum = sum + i;
}
System.out.println("The sum of the first three positive integers is " + sum + ".");
}
}
Close
The product of the first five positive integers is 120.
Close
public class Loops {
public static void main(String[] args) {
int product = 1;
for (int i = 1; i <= 5; i++) {
product *= i;
}
System.out.println("The product of the first five positive integers is " + product + ".");
}
}
Close
Assuming the user inputs the number 7, the output should be:
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
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
}
}
}
Close
Assuming the user inputs the number 6, the output should be:
The sum of all even numbers from 1 to 6 is 12.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer:");
int number = scanner.nextInt();
int sum = 0;
if (number < 1) {
System.out.println("Please insert a positive integer");
}
else {
for (int i = 2; i <= number; i+=2) {
sum = sum + i;
}
}
System.out.println("The sum of all even number from 1 to " + number + " is " + sum);
}
}
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer:");
int number = scanner.nextInt();
if (number < 1) {
System.out.println("Please insert a positive integer.");
}
else {
for (int i = number; i >= 1; i--) {
System.out.println(i);
}
}
}
}
Close
Assuming the user inputs the number 28, the output should be:
The number 28 is a perfect number.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer between 1 and 500: ");
int number = scanner.nextInt();
int sumOfDivisors = 0;
if (number < 1 || number > 500) {
System.out.println("Please insert a valid integer");
}
else {
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sumOfDivisors = sumOfDivisors + i;
}
}
if (sumOfDivisors == number) {
System.out.println("The number " + number + " is a perfect number");
}
else {
System.out.println("The number " + number + " is a not perfect number");
}
}
}
}
Close
Assuming the base is 2 and the exponent is 3, the output should be:
2 raised to the power of 3 equals 8.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a base number: ");
int base = scanner.nextInt();
System.out.print("Enter an exponent: ");
int exponent = scanner.nextInt();
int result = 1;
for (int i = 1; i<= exponent; i++) {
result = result * base;
}
System.out.println(base + " raised to the power of " + exponent + " equals " + result);
}
}
Close
Assuming the user inputs the numbers 5, 10, 15, and then -1 to stop, the output should be:
The total sum of the positive numbers entered is 30.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
System.out.println("Enter a positive integer to sum up. Enter a negative integer to stop.");
while (true) {
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number < 0) {
break;
}
else {
sum = sum + number;
}
}
System.out.println("The total sum of the positive integers entered is " + sum);
}
}
Close
Assuming the user inputs the numbers 5, 7, 8, 4, 2, and then 0 to stop, the output should be:
The count of all even integers from this set of numbers is 3.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a series of integers. Input 0 to stop.");
int count = 0;
while (true) {
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (number == 0) {
break;
}
else if (number % 2 == 0) {
count = count + 1;
}
}
System.out.println("The count of all even integers from this set of numbers is " + count);
}
}
Close
Assuming the user inputs the numbers 3, 5, 7, 2, 8, 5, and then -2 to stop, the output should be:
The average of all odd numbers from this set is 5.
Close
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter positive integers. Enter a negative integer to stop.");
int sum = 0;
int count = 0;
while (true) {
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (number < 0) {
break;
}
else if (number % 2 != 0) {
sum = sum + number;
count = count + 1;
}
}
if (count > 0) {
double average = (double) sum / count;
System.out.println("The average of all odd numbers from this set is " + average);
}
else {
System.out.println("No odd numbers were entered");
}
}
}
Close