Respuesta :
Answer:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int firstNum, secondNum;
- do{
- System.out.print("Input first number: ");
- firstNum = input.nextInt();
- System.out.print("Input second number: ");
- secondNum = input.nextInt();
- for(int i= firstNum; i <= secondNum; i++){
- if(i % 2== 1){
- System.out.print(i + " ");
- }
- }
- System.out.println();
- int evenSum = 0;
- for(int i= firstNum; i <= secondNum; i++){
- if(i % 2== 0){
- System.out.print(i + " ");
- evenSum += i;
- }
- }
- System.out.println();
- System.out.println("Sum of even: " + evenSum);
- System.out.println();
- int squareOddSum = 0;
- for(int i= firstNum; i <= secondNum; i++){
- System.out.println(i + " : " + (i*i) );
- if(i % 2 ==1){
- squareOddSum += i;
- }
- }
- System.out.println();
- System.out.println("Sum of squared odd number: " + squareOddSum);
- }while(firstNum < secondNum);
- }
- }
Explanation:
The solution code is written in Java.
Firstly, we create a Scanner object (Line 6) and use it get user input for first and second number (Line 10-13). We create a for loop and use modulus to check the current number is divisible by 2 and print out the odd number in loop (Line 15-19). We repeat the similar step to print the even number but at the same time, we also add the current number to evenSum variable and print the sum after the for loop (Line 23-33).
Next, we proceed to use another for loop to print out the number and their squares and then accumulate the sum of squares of all odd numbers (Line 36-41).
At last, we print out the sum of squares of odd numbers (Line 43).
In the do while loop condition, if firstNum is smaller than secondNumber, the loop is ongoing (Line 45).