-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab03_B_DoWhile.java
More file actions
54 lines (42 loc) · 1.31 KB
/
Lab03_B_DoWhile.java
File metadata and controls
54 lines (42 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class Lab03_B_DoWhile {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int firstNum = 1;
int secondNum = 0;
int currentNum = 0;
while(firstNum >= secondNum)
{
System.out.println( "Input the first number:");
firstNum = console.nextInt();
currentNum=firstNum;
System.out.println( "Input the second number:");
secondNum = console.nextInt();
if (firstNum >= secondNum)
{
System.out.println("Sorry, your first number must be less than the second.");
}
}
System.out.println( "All odd numbers between " + firstNum + " and " + secondNum + " inclusive");
do
{
if(currentNum %2 == 1) {
System.out.println(currentNum);
}
currentNum++;
}
while (currentNum <= secondNum);
System.out.println("Sum of all even numbers between " + firstNum + " and " + secondNum + " inclusive");
int sum = 0;
currentNum = firstNum;
do
{
if(currentNum %2 == 0) {
sum = (sum + currentNum);
}
currentNum++;
}
while (currentNum <= secondNum);
System.out.println(sum);
}
}