This blog is under construction

Sunday, 23 June 2013

C program to print Adam numbers from 1 to 1000

What is an Adam Number?
If the square of a number and the reverse of square of the same number are palindrome to each other, then the number is Adam number.
12^2 = 144
21^2 = 441
144 is the palindrome of 441
441 is the palindrome of 441

So, 12 is an Adam number.

Write a C program to generate Adam Numbers.


  #include <stdio.h>

  /* find reverse of the given number */
  int reverse (int num) {
        int rev = 0, mod;
        while (num > 0) {
                mod = num % 10;
                rev = (rev * 10) + mod;
                num = num / 10;
        }
        return rev;
  }

  int main() {
        int n, i, num, rev, num_sq, rev_sq;
        printf("Result:\n");
        /* checking whether the input number is adam or not */
        for (i = 0; i < 1000; i++) {
                num = i;
                rev = reverse(num);
                num_sq = num * num;
                rev_sq = rev * rev;
                /* print adam numbers alone */
                if (num_sq == reverse(rev_sq)) {
                        printf("\n%03d is an Adam number\n", num);
                        printf("square of %d is %d\n", num, num_sq);
                        printf("square of %d is %d\n", rev, rev_sq);
                }
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Result:
  000 is an Adam number
  square of 0 is 0
  square of 0 is 0

  001 is an Adam number
  square of 1 is 1
  square of 1 is 1

  002 is an Adam number
  square of 2 is 4
  square of 2 is 4

  003 is an Adam number
  square of 3 is 9
  square of 3 is 9

  011 is an Adam number
  square of 11 is 121
  square of 11 is 121
  
  012 is an Adam number
  square of 12 is 144
  square of 21 is 441

  013 is an Adam number
  square of 13 is 169
  square of 31 is 961

  021 is an Adam number
  square of 21 is 441
  square of 12 is 144

  022 is an Adam number
  square of 22 is 484
  square of 22 is 484

  031 is an Adam number
  square of 31 is 961
  square of 13 is 169

  101 is an Adam number
  square of 101 is 10201
  square of 101 is 10201

  102 is an Adam number
  square of 102 is 10404
  square of 201 is 40401

  103 is an Adam number
  square of 103 is 10609
  square of 301 is 90601

  111 is an Adam number
  square of 111 is 12321
  square of 111 is 12321

  112 is an Adam number
  square of 112 is 12544
  square of 211 is 44521

  113 is an Adam number
  square of 113 is 12769
  square of 311 is 96721

  121 is an Adam number
  square of 121 is 14641
  square of 121 is 14641

  122 is an Adam number
  square of 122 is 14884
  square of 221 is 48841

  201 is an Adam number
  square of 201 is 40401
  square of 102 is 10404
  
  202 is an Adam number
  square of 202 is 40804
  square of 202 is 40804

  211 is an Adam number
  square of 211 is 44521
  square of 112 is 12544

  212 is an Adam number
  square of 212 is 44944
  square of 212 is 44944

  221 is an Adam number
  square of 221 is 48841
  square of 122 is 14884

  301 is an Adam number
  square of 301 is 90601
  square of 103 is 10609

  311 is an Adam number
  square of 311 is 96721
  square of 113 is 12769



1 comment: