do while loop
Sample output:
1. Write a program that asks the user for a starting value and an ending
value and then writes all the integers (inclusive) between those two values.
Enter Start:
10
Enter End:
15
10
11
12
13
14
15
Sum of in range values: 75
public class Loop {
public static void main(String[] args) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.println("Enter start:");
int current = scanner.nextInt();
System.out.println("Enter end:");
int end = scanner.nextInt();
int sum = 0;
do {
System.out.println(current);
sum += current;
current++;
} while (current <= end);
System.out.println("Sum of in range values: " + sum);
}
}
Comments
Leave a comment