# use while loop to output the sum of the square of odd numbers

on
First, i prompt the user to input 2 integers: firstNum & secondNum (firstNum must be less than secondNum)
this is easy.
then,
i have to output the sum of the square of odd numbers between these two integers,
so i write the codes like this:

import java.io.*;
import java.util.*;
import java.math.*;

public class OddSquare
{

public static void main(String[] args) throws IOException
{
double firstNum, secondNum, n, square;

n = 0;

System.out.flush();

System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();

while(firstNum < secondNum)
{
if(firstNum%2 == 0)
{
firstNum = firstNum + 2;
square = Math.pow(firstNum, 2);
System.out.print(square + " ");
}
else
{
firstNum = firstNum + (2*n + 1);
n++;
square = Math.pow(firstNum, 2);
System.out.print(square + " ");
}
}
}
}

i input 3 and 17, then the ouput is unexpected:
"16.0 36.0 64.0 100.0 144.0 196.0 256.0 324.0"

why?
i don't know which part i did wrong?

by the way,
can anyone give me a hint of how to "output all uppercase letters"?

thanks
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:

For the uppercase letters:

for (char i = 'A'; i <= 'Z'; i++)
{
System.out.print(i + " ");
}
System.out.println();

Commented:
import java.io.*;
import java.math.*;
import java.util.*;

public class ch5_num8_partE_SumOfSquareOfOddNumbers
{

public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n, sum, rem;

double result;

System.out.flush();

System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();

rem = firstNum % 2;

while (++firstNum < secondNum)
{
if (firstNum % 2 != 0)
result = Math.pow(firstNum,2);
System.out.println(result + " ");
firstNum++;
}
}
}

there's still one error on the "result".......................

Commented:
You're missing some braces and you are incrementing firstNum in two places.

Try the following:

while (firstNum < secondNum)
{
if (firstNum % 2 != 0)
{
result = Math.pow(firstNum,2);
System.out.println(result + " ");
}
firstNum++;
}

Commented:
input:
3
17

the output becomes:
9.0
25.0
49.0
.
.
.
.
225.0

the "9.0" shouldn't appear..................
cause 3 isn't between 3 and 17.................

Commented:
That's OK.  Just add the following line before the "while"

firstNum++;

Commented:
darn! i should have known this.
thank you so much, jimmack

Commented:
;-)

Don't forget, your question wants the "sum" of the squares.

Create another variable:

double squareSum = 0.0;

Then, inside the loop, between "result = " and "System.out...", do

squareSum += result;

Commented:
/*
*Filename:    ch5_num8_partE_SumOfSquareOfOddNumbers
*Versoon:     v1.0
*Description: This program uses a while loop to:
*                 1) Prompt the user to input two integers: firstNum and secondNum.
*                   2) Output the sum of the square of odd numbers between firstNum
*                          and secondNum.
*Author:         Daniel Yen-Chung Chen
*Created on:  October 22, 2003
*/

import java.io.*;
import java.math.*;
import java.util.*;

public class ch5_num8_partE_SumOfSquareOfOddNumbers
{

public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n, sum, rem;

double result;

double squareSum = 0.0;

System.out.flush();

System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();

rem = firstNum % 2;

firstNum++;

while (firstNum < secondNum)
{
if (firstNum % 2 != 0)
{
result = Math.pow(firstNum,2);
squareSum += result;
System.out.println(result + " ");
}

firstNum++;
}

}
}

............
now the output is:
25.0
49.0
81.0
121.0
169.0
225.0
shouldn't i add the "System.out.println(squareSum);" after the while loop?

Commented:
Yes.

Try:

System.out.println("Sum of squares = " + squareSum);

(It's a bit more descriptive)

Commented:
thank you^^

Do more with