422,341 views
35 votes
35 votes
Unit 4: Lesson 2 - Coding Activity 1

Ask the user for two numbers. Print only the even numbers between them. You should also print the two numbers if they are even.

Starter code:
import java.util.Scanner;

public class U4_L2_Activity_One{
public static void main(String[] args){

Scanner scan = new
Scanner(System.in);
System.out.println("Enter two numbers:");
int num1 = scan.nextlnt();
int num2 = scan.nextlnt();
while (num1 <= num2){
if (num1 %2==0){
system.out.print(num1="");
}
num+=1;
}
}
}

User Shalva Kakauridze
by
2.5k points

2 Answers

15 votes
15 votes

Final answer:

The given code is a Java program that asks the user for two numbers and prints all the even numbers between them.

Step-by-step explanation:

The given code is a Java program that asks the user for two numbers and prints all the even numbers between them.

To achieve this, the program uses a while loop to iterate from the first number to the second number. Inside the loop, it checks if each number is even using the modulo operator (%). If a number is even, it is printed using the System.out.print() method.

Here is the corrected code:

import java.util.Scanner;

public class U4_L2_Activity_One {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers:");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
while (num1 <= num2) {
if (num1 % 2 == 0) {
System.out.print(num1 + " ");
}
num1 += 1;
}
}
}

User Mikijov
by
3.6k points
11 votes
11 votes

Answer:

System.out.println("Enter two numbers:");

int num1 = scan.nextlnt();

int num2 = scan.nextlnt();

Step-by-step explanation:

User Hdx
by
3.2k points