Question

Using Java loops Write a program that finds all the perfect squares and cubes of numbers...

Using Java loops

Write a program that finds all the perfect squares and
cubes of numbers from 1 to a number entered by the
user.

If the number it is examining is a perfect square, it
prints the number user followed by “ is a perfect
square”

If the number it is examining is a perfect cube, it
prints the number user followed by “ is a perfect
cube”

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class PerfectSquaresCubes {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a max value: ");
        int n = in.nextInt();
        for (int i = 2; i <= n; i++) {
            for (int j = 2; j <= n; j++) {
                if (j*j == i) {
                    System.out.println(i + " is a perfect square");
                }
                if (j*j*j == i) {
                    System.out.println(i + " is a perfect cube");
                }
            }
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Using Java loops Write a program that finds all the perfect squares and cubes of numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT