KnowledgeBoat Logo

Java Number Programs (ICSE Classes 9 / 10)

Write a program to display all the numbers between m and n input from the keyboard (where m<n, m>0, n>0), check and print the numbers that are perfect square. e.g. 25, 36, 49, are said to be perfect square numbers.

Java

Java Iterative Stmts

113 Likes

Answer

import java.util.Scanner;

public class KboatPerfectSquare
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter m: ");
        int m = in.nextInt();
        System.out.print("Enter n: ");
        int n = in.nextInt();
        
        if (m < n && m > 0 && n > 0) {
            for (int i = m; i <= n; i++) {
                System.out.println("Number = " + i);
                double sroot = Math.sqrt(i);
                if (sroot == Math.floor(sroot))
                    System.out.println(i + " is a perfect square");    
            }
        }
        else {
            System.out.println("Invalid input");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to display all the numbers between m and n input from the keyboard (where m n, m>0, n>0), check and print the numbers that are perfect square. e.g. 25, 36, 49, are said to be perfect square numbers.

Answered By

39 Likes