alter the example so that it Add 1 to i. return 0; EXERCISES Exercise 2.2.1. Wri
ID: 3590246 • Letter: A
Question
alter the example so that it Add 1 to i. return 0; EXERCISES Exercise 2.2.1. Write a program to print all the numbers from nl to n2, where and n2 are two numbers specified by the user. (Hint: You'l need to romnt fo two values nl and n2; then, initialize i to nl and use n2 in the loop condition Exercise 2.2.2. Alter the example so that it prints all the numbers from n to lin reverse order, as in 5 432 1. (Hint: To decrement a value inside the loop, usethe statement "i = i-1;") Exercise 2.2.3. Alter the example so that it prints only even numbers, as in 0, 2,4 Hint: One of the things you'll need to do is initialize i to 0.)Explanation / Answer
PrintValues.java
import java.util.Scanner;
public class PrintValues {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the n1: ");
int n1 = scan.nextInt();
System.out.println("Enter the n2: ");
int n2 = scan.nextInt();
for(int i=n1;i<=n2;i++) {
System.out.print(i+" ");
}
System.out.println();
System.out.println("Enter the n value: ");
int n = scan.nextInt();
System.out.println();
for(int i=n;i>=1;i--) {
System.out.print(i+" ");
}
System.out.println();
System.out.println("Even Numbers: ");
for(int i=0;i<=n;i++) {
if(i%2 ==0){
System.out.print(i+" ");
}
}
System.out.println();
}
}
Output:
Enter the n1:
5
Enter the n2:
10
5 6 7 8 9 10
Enter the n value:
10
10 9 8 7 6 5 4 3 2 1
Even Numbers:
0 2 4 6 8 10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.