1.9k views
3 votes
Write a program in Java whose input is two integers, and whose output is the first integer and subsequent increments of 5 as long as the value is less than or equal to the second integer. End with a newline.

User LONGI
by
8.4k points

1 Answer

1 vote

Final answer:

To write a program in Java that prints the first integer and increments of 5 until the second integer, you can use a while loop.

Step-by-step explanation:

To write a program in Java with the given input and output requirements, you can use a loop to iterate and increment the first integer by 5 until it reaches the second integer. Here is an example:

import java.util.Scanner;

public class IncrementProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter the first integer: ");
int firstInt = input.nextInt();

System.out.print("Enter the second integer: ");
int secondInt = input.nextInt();

while (firstInt <= secondInt) {
System.out.println(firstInt);
firstInt += 5;
}
}
}

In this program, the user is prompted to enter the first and second integers. The while loop prints the current value of firstInt and then increments it by 5 until it becomes greater than secondInt. Each value is printed on a new line.

User Stan James
by
7.6k points