Question #248029

Draw a flowchart that uses while loops to perform the following steps:

a. input two integers: firstNum and secondNum.

b. all the odd numbers between firstNum and secondNum inclusive.

c. the sum of all the even numbers between firstNum and secondNum inclusive.

d. all the numbers and their squares between 1 and 10.

e. the sum of the squares of all the odd numbers between firstNum and secondNum inclusive.


Expert's answer

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    Scanner in=new Scanner(System.in);
	    int firstNum, secondNum;
	    //a. input two integers: firstNum and secondNum.
	    System.out.println("Enter first number: ");
	    firstNum=in.nextInt();
	    System.out.println("Enter second number: ");
	    secondNum=in.nextInt();
	    //b. all the odd numbers between firstNum and secondNum inclusive.
	    //c. the sum of all the even numbers between firstNum and secondNum inclusive.
	    int sumOdd=0;
	    int sumEven=0;
	    int i=firstNum;
	    
	    while(i<=secondNum){
	        if (i%2==0)
	            sumEven+=i;
	        else
	            sumOdd+=i;
	        i+=1;
	    }
	    System.out.println("Sum of odd numbers: "+sumOdd);
	    System.out.println("Sum of even numbers: "+sumEven);
	    // all the numbers and their squares between 1 and 10.
	    int square;
	    for (i = 1; i<=10; i++)
		{
    		square = i * i;
    		System.out.println("Number\t\t\tSquare of number");
    		System.out.println(i+"\t\t\t"+square);
		}
		//e. the sum of the squares of all the odd numbers between firstNum and secondNum inclusive.
		int sum2=0;
		for (i = firstNum; i < secondNum; i++)
		{
			if (i % 2 != 0)
			{
				sum2 = sum2 + (i * i);
			}
		}
		System.out.println("The Sum of the squares of the odd integers: "+sum2);
	}
}
LATEST TUTORIALS
APPROVED BY CLIENTS