Author Topic: Java Adam Number Source Code  (Read 4800 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Java Adam Number Source Code
« on: January 23, 2012, 12:48:18 pm »
Here is the Java Program to print the list of Adam Numbers.

The logic for finding the adam number is given below:

Lets take a number: 12
Find the square for this number: 144
Find the square for the reverse of this number(21): 441
In the above two results - 144 and 441, 441 is the reverse of 144, then the number 12 is called Adam Number.

Lets take a number: 15
Find the square for this number: 225
Find the square for the reverse of this number(51): 2601
In the above two results - 225 and 2601, 2601 is not the reverse of 225, then the number 15 is NOT a  Adam Number.

Code: (java)
//Source Code Listing


import java.lang.*;
import java.util.*;
import java.io.*;
 
 
class AdamNumber
{
      int CountNumberOfDigits(int n)
      {
         int numdgits = 0;
         do
         {
            n = n / 10;
            numdgits++;
         }
         while (n > 0);
         return numdgits;
      }

      int ReverseNumber(int n)
      {
         int i = 0, result = 0;
         int numdigits = CountNumberOfDigits(n);
         for (i = 0; i < numdigits; i++)
         {
            result *= 10;
            result += n % 10;
            n = n / 10;
         }
         return result;
      }
 
      public static void main(String[] args)
      {      
         System.out.println("Enter the total numbers:");
         AdamNumber an = new AdamNumber();
         int i, n, rn;
         int sn, rsn, rrsn;
         System.out.println("List of Adam Numbers under 1000\n");
         for (i = 10; i < 1000; i++)
         {
            n = i;
            rn = an.ReverseNumber(i);
            if (n == rn)
               continue;
            sn = n * n;
            rsn = rn * rn;
            rrsn = an.ReverseNumber(rsn);
            if (rrsn == sn)
            {
               System.out.format("%3d\n", n);
            }
         } 
      }
}

/***
output:
List of Adam Numbers under 100012
13
21
31
102
103
112
113
122
201
211
221
301
311

***/


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1