0

My homework assignment is to do the following steps below. I am able to write steps 1,2,3, and 5. I am stuck on how to go about writing code for step 4. Can I include this step into one of my for loops or do I need to write another loop?

Write a program that uses while loops to perform the following steps:
1. Prompts the user to input two integers:firstNum and secondNum.(firstNum must be less than secondNum).
2.Outputs all the odd numbers between firstNum and secondNum.
3.Outputs the sum of all the even numbers between firstNum and secondNum.
4. Outputs all the numbers that when squared are between 1 and 10. So if firstNum is greater than 3 there won't be any numbers that when squared are between 1 and 10.
5. Output the sum of the squares of all the odd numbers between firstNum and secondNum.

Here is what I have written thus far:

import java.util.*;

public class Assignment5
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {

		int num1;
		int num2;
		int num = 0;
		int even = num;
		int square = 0 ;
		int sumSquare = 0;


		System.out.println("Please enter a number between 1 and 5.");

		num1 = console.nextInt();

		System.out.println("Please enter a second number between 6 and 10");

		num2 = console.nextInt();

		for (num = num1; num <= num2; num++){
			 if ( num %2 != 0 ){
				 System.out.println("Odd numbers between" + " " + num1 + " " + "and" + " " + num2 + " " + "are" + " " + num + " ");
				 sumSquare+=(num*num);

			 } else{
			    even = even + num;


		     }
	   }

	   System.out.println("Sum of the even numbers between" + " " + num1 + " " + "and" + " " + num2 + " " + "are" + " " + even + " ");
       System.out.println("Sum of squares of all odd numbers between" + " " + num1 + " " + "and" + " " + num2 + "=" + sumSquare);


   }
}
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Slyvr
0

4. Outputs all the numbers that when squared are between 1 and 10. So if firstNum is greater than 3 there won't be any numbers that when squared are between 1 and 10.

for (int i=0; i<totalnumbers; i++){
squarednumber = number*number;
if ((squarednumber>=1) && (squarednumber<=10)){
System.out.println(squarednumber);
}
}

Edited by Slyvr: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.