** USING WHILE LOOPS ONLY + public class while loops * Given two integers, the second larger than the first, this program: * 1) prints the even numbers between the first and second number inclusive * 2) prints the sum of those even numbers * 3) prints the odd numbers between the first and second number inclusive * 4) prints the sum of those odd numbers * * Example: * Enter an integer: 10 * Enter one larger than the first: 20 * Even numbers: 10 12 14 16 18 20 * Sum of even numbers = 90 * Odd numbers: 11 13 15 17 19 * Sum of odd numbers = 75
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
/** USING WHILE LOOPS ONLY + public class while loops
* Given two integers, the second larger than the first, this
* 1) prints the even numbers between the first and second number inclusive
* 2) prints the sum of those even numbers
* 3) prints the odd numbers between the first and second number inclusive
* 4) prints the sum of those odd numbers
*
* Example:
* Enter an integer: 10
* Enter one larger than the first: 20
* Even numbers: 10 12 14 16 18 20
* Sum of even numbers = 90
* Odd numbers: 11 13 15 17 19
* Sum of odd numbers = 75
**the image below also states the requirements**
![### WhileLoops.java
```java
/**
* Given two integers, the second larger than the first, this program:
* 1) prints the even numbers between the first and second number inclusive
* 2) prints the sum of those even numbers
* 3) prints the odd numbers between the first and second number inclusive
* 4) prints the sum of those odd numbers
*
* Example:
* Enter an integer: 10
* Enter one larger than the first: 20
* Even numbers: 10 12 14 16 18 20
* Sum of even numbers = 90
* Odd numbers: 11 13 15 17 19
* Sum of odd numbers = 75
*
* This version uses WHILE loops.
*
* @author
* @version
*/
import java.util.Scanner;
public class WhileLoops
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
int firstNum = in.nextInt();
System.out.print("Enter one larger than the first: ");
int secondNum = in.nextInt();
// Complete the program
}
}
```
### Explanation for Educational Context:
This Java program, `WhileLoops.java`, is designed to perform the following tasks using a WHILE loop:
1. Print all even numbers between two user-entered integers (inclusive).
2. Calculate and print the sum of these even numbers.
3. Print all odd numbers between the same two integers (inclusive).
4. Calculate and print the sum of these odd numbers.
**Example Interaction:**
If the user enters `10` as the first number and `20` as the second number:
- The even numbers between 10 and 20 are: 10, 12, 14, 16, 18, 20.
- The sum of these even numbers is: 10 + 12 + 14 + 16 + 18 + 20 = 90.
- The odd numbers between 10 and 20 are: 11, 13, 15, 17, 19.
- The sum of these odd numbers is: 11 + 13 + 15 + 17 + 19 = 75.
### Implementation Steps:
1. Initialize a `Scanner` object to read user input.
2](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fd772767f-fd91-4ca5-b737-b5766e3fef0a%2F23fc7e9b-c5df-4451-9f67-e4dc2f0faadf%2Fa48nf3f.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)