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
import java.util.Scanner;
class TablesLab1 {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter Start:");
int start=keyBoard.nextInt();
System.out.print("Enter End:");
int end=keyBoard.nextInt();
int counter=start;
int sum=0;
do{
System.out.println(counter);
sum+=counter;
counter++;
}while(counter<=end);
System.out.println("Sum of in range values: "+sum);
keyBoard.close();
}
}
Comments
Leave a comment