Answer to Question #248029 in Java | JSP | JSF for kayjay

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.


1
Expert's answer
2021-10-09T04:45:04-0400
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);
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS