Software & Finance Forums

Author Topic: Turbo C Program to print the list of Adam Numbers  (Read 4576 times)

kathir

• Sr. Member
• Posts: 283
Turbo C Program to print the list of Adam Numbers
« on: January 11, 2012, 12:05:24 pm »
Here is the Turbo 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:
//Source Code Listing

#include <stdio.h>
#include <conio.h>
#include <math.h>

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;
}

void main()
{
int i, n, rn;
int sn, rsn, rrsn;
printf("List of Adam Numbers under 1000\n");
for(i = 10; i < 1000; i++)
{
n = i;
rn = ReverseNumber(i);
if(n == rn)
continue;
sn = n * n;
rsn = rn * rn;
rrsn = ReverseNumber(rsn);
if(rrsn == sn)
{
printf("%d\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.