Author Topic: C# Adam Number  (Read 1550 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# Adam Number
« on: January 23, 2012, 12:33:44 pm »
Here is the C# 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: (c#)
//Source Code Listing

using System;
using System.Collections.Generic;
using System.Text;

namespace CSTest
{  
   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;
      }     

      static void Main(string[] args)
      {
         System.Console.WriteLine();
         AdamNumber an = new AdamNumber();
         int i, n, rn;
         int sn, rsn, rrsn;
         System.Console.WriteLine("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.Console.WriteLine("{0,3}", n);
            }
         } 

      }
   }
}
/*** 
output: 
List of Adam Numbers under 1000
 12
 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