Solved

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

Posted on 2003-10-27
10
797 Views
Last Modified: 2011-09-20
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
{
      static BufferedReader keyboard =
                        new BufferedReader(new InputStreamReader(System.in));
      
      public static void main(String[] args) throws IOException
      {
            double firstNum, secondNum, n, square;
            
            n = 0;
            
            System.out.print("please enter an integer: ");
            System.out.flush();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("please enter another one that is larger than the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            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
0
Comment
Question by:crystalfish0318
  • 5
  • 5
10 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 20 total points
ID: 9626075
See your previous questions for clues about the output.

For the uppercase letters:


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

Author Comment

by:crystalfish0318
ID: 9626222
import java.io.*;
import java.math.*;
import java.util.*;

public class ch5_num8_partE_SumOfSquareOfOddNumbers
{
      static BufferedReader keyboard =
             new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException
      {
            int firstNum, secondNum, n, sum, rem;
            
            double result;
            
            System.out.print("please enter an integer: ");
            System.out.flush();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("please enter another one that is larger than the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            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".......................
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9626243
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++;
           }

0
 

Author Comment

by:crystalfish0318
ID: 9626280
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.................
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9626293
That's OK.  Just add the following line before the "while"

firstNum++;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:crystalfish0318
ID: 9626310
darn! i should have known this.
thank you so much, jimmack
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9626345
;-)

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;

0
 

Author Comment

by:crystalfish0318
ID: 9626400
/*
 *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
{
      static BufferedReader keyboard =
             new BufferedReader(new InputStreamReader(System.in));

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

            
            System.out.print("please enter an integer: ");
            System.out.flush();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("please enter another one that is larger than the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            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?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9626407
Yes.

Try:

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

(It's a bit more descriptive)
0
 

Author Comment

by:crystalfish0318
ID: 9626411
thank you^^
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 124
how do i compare an object based on two fields 6 49
session migration servlets 2 26
MySqlDump not dumping triggers 1 15
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now