PRASHNIKAप्रश्निका
‹ Back to the paper

[Programme based on Numbers] An Automorphic number is a number whose square ends with the given…

Computer Science202510 marksProgram
[Programme based on Numbers] An Automorphic number is a number whose square ends with the given number itself. E.g. $(5)^2 = 25$, $(25)^2 = 625$, $(76)^2 = 5776$. Design a class Automorphic to check if numbers in the given range are Automorphic numbers or not. The member functions and data members of the class are given below: Class Name : Automorphic Data members/instance variables: l, u : lower and upper limits of the range count : frequency of the automorphic numbers in the range l and u Member functions : Automorphic(int l, int u) : parameterised constructor to assign values to data members boolean check(int n) : to check if n is an automorphic number. If yes it returns true otherwise returns false. void list( ) : to check all the numbers between the range l and u, by calling check(int n) and to display only the automorphic numbers. Also to display the frequency of the automorphic numbers in the given range. Specify the class Automorphic giving details of the parameterised constructor, boolean check(int n) and void list( ). Create one object in the main( ) method and call all the methods appropriately. Sample input Enter the lower limit of the range->1 Enter the upper limit of the range->1000 Sample output List of Automorphic numbers from 1 to 1000 :
NumberSquare
11
525
636
25625
765776
376141376
625390625
Frequency of Automorphic numbers between 1 to 1000 : 7

Sample input/output

Sample input
Enter the lower limit of the range->1
Enter the upper limit of the range->1000

Sample output
List of Automorphic numbers from 1 to 1000 :
| Number | Square |
| :--- | :--- |
| 1 | 1 |
| 5 | 25 |
| 6 | 36 |
| 25 | 625 |
| 76 | 5776 |
| 376 | 141376 |
| 625 | 390625 |
Frequency of Automorphic numbers between 1 to 1000 : 7

Answer

Answer

AI
import java.util.Scanner;

class Automorphic
{
    int l, u;      // lower and upper limits of the range
    int count;     // frequency of automorphic numbers in the range

    // parameterised constructor
    Automorphic(int l, int u)
    {
        this.l = l;
        this.u = u;
        count = 0;
    }

    // returns true if n is an automorphic number
    boolean check(int n)
    {
        long sq = (long) n * n;
        String ns = Integer.toString(n);
        String sqs = Long.toString(sq);
        return sqs.endsWith(ns);
    }

    // checks every number in the range, displays the automorphic ones and their frequency
    void list()
    {
        System.out.println("List of Automorphic numbers from " + l + " to " + u + " :");
        System.out.println("Number\tSquare");
        count = 0;
        for (int i = l; i <= u; i++)
        {
            if (check(i))
            {
                long sq = (long) i * i;
                System.out.println(i + "\t" + sq);
                count++;
            }
        }
        System.out.println("Frequency of Automorphic numbers between " + l + " to " + u + " : " + count);
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the lower limit of the range->");
        int l = sc.nextInt();
        System.out.print("Enter the upper limit of the range->");
        int u = sc.nextInt();
        Automorphic ob = new Automorphic(l, u);
        ob.list();
    }
}
Explanation: check(n) compares the string form of n's square against the string form of n itself - n is automorphic exactly when its square's string ends with n's own digits (this avoids any issue with leading digits and works for any size square by using long). list() calls check() on every number from l to u, prints the number and its square for each automorphic one, and counts them. Tested (run for real) with l=1, u=1000: the program printed the numbers 1, 5, 6, 25, 76, 376, 625 with their squares and "Frequency of Automorphic numbers between 1 to 1000 : 7", matching the sample output exactly.
Objects

From ISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024), question 71.