Subscribe

RSS Feed (xml)

Monday, February 16, 2009

Write a C program to check whether the given integer is Magic number or not.

Magic number:- A number which is divisible by 9 and also after reverse if it is divisible by 9, then it is said to Magic number.

Example:- Suppose take 18
We know '18' is divisible by 9 (ie., 9 * 2 = 18)
Now After reverse '18' becomes 81.
Here '81' is also is divisible by 9 (ie., 9 * 9 =81 )
So we say '18' is a Magic number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, rev=0, n;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=(10*rev)+ rem;
n=n/10;
}
if(rev%9== 0)
printf("\nMagic Number.");
else
printf("\nNot a Magic number.");
getch( );
}

Output:-
Enter the required number: 27
Magic Number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

29 comments:

  1. i want to know about the whole meaning of the program cause I've tried this but its not working

    ReplyDelete
  2. what is mean by magic no.

    ReplyDelete
  3. i want to print all the numbers which are magic numbers how can i write it

    ReplyDelete
  4. no..its wrong logic
    the actual logic is ...
    suppse we have no. 9
    its square 81...divide this no in two parts so....8 and 1
    and then addition between them ...you get 9 again...is called magic no.
    ex. no. 99 also magic no.
    so 99*99=9801...dividing the no in two part ...
    so 98 and 01....now addition between then...98+01 you get 99 again

    ReplyDelete
  5. magic numbers like 153,
    1^3 + 5^3 + 3^3=153 =>1+125+27=153
    the cube of all digits is = the same digit.



    the code is below
    public void magic()
    {
    int a = 53, b,rev=0,s=0;
    int m=a, j=0;
    while (a > 0)
    {
    b = a % 10;
    rev = (b * b* b);

    j = j + rev;
    a = a / 10;
    }

    if (j == m)
    Console.WriteLine("the no is a magic no");
    else
    Console.WriteLine("not magic no");

    ReplyDelete
    Replies
    1. this is not magic number dude, it is called armstrong no...

      Delete
  6. Jagadish Sati : Actually this is the logic for armstrong number.

    ReplyDelete
  7. I want to know...that how to initialize the counter in for loop..mainy iI have problem that sometimes we start it from number entered from user and often from 1 .....why..?

    ReplyDelete
  8. I learn totally different about magic number...

    ReplyDelete
  9. A number is magical if repeated adding of its digit gives 1. example 19 is magical
    as 1 + 9 = 10, 1 + 0
    = 1 hence magical. So is 991 as 9 + 9 + 1 = 19, 1 + 9 = 10, 1 + 0 = 1. However 224
    is not.

    ReplyDelete
    Replies
    1. Input a range from user and print all the magic numbers in that range.





      #include
      int main()
      {
      int low=0,high=0;
      printf("Enter the range: low,high");
      scanf("%d,%d",&low,&high);

      int i=0;
      for(i = low; i<= high ;i++)
      {
      int n = i,sum = n;

      while(sum > 9)
      {
      n = sum;
      sum = 0;
      while(n > 0)
      {
      int digit = n%10;
      n = n/10;
      sum = sum + digit;
      }

      }
      if(sum == 1)
      {
      printf(" %d is a magic number \n",i);
      }
      }

      return 0;
      }


      Delete
    2. You are the correct man

      Delete
  10. I am agree with Pawan Pandey about Magic number that sum must be 1 at the end.

    ReplyDelete
  11. Instead of trying to put me down to feel Good about yourself why don’t you try to make some achievements yourself to feel better?"

    ReplyDelete
  12. Might be helpfull to sombody in need: if anybody want to install mac os x directly in non apple laptop without using vmware/virtualbox

    pawanpandeya07.blogspot.in

    ReplyDelete
  13. This is the correct program


    #include

    /* sum of digits of a number */
    int sumOfDigits(int num) {
    int sum = 0;
    while (num > 0) {
    sum = sum + (num % 10);
    num = num / 10;
    }
    return sum;
    }

    /* returns reverse of a given number */
    int reverse(int num) {
    int rev = 0;
    while (num > 0) {
    rev = (rev * 10) + (num % 10);
    num = num / 10;
    }
    return rev;
    }

    int main () {
    int num, sum, rev;

    /* get the input value from the user */
    printf("Enter the value for num:");
    scanf("%d", &num);

    /* find sum of digits */
    sum = sumOfDigits(num);

    /*
    * if the value is single digit, then
    * the value and its reverse are same
    */
    if (sum < 10) {
    if ((sum * sum) == num) {
    printf("%d is a magic number\n", num);
    } else {
    printf("%d is not a magic number\n", num);
    }
    return 0;
    }

    /* reverse of the given number */
    rev = reverse(sum);

    /* print the outputs */
    if ((sum * rev) == num) {
    printf("%d is a magic number\n", num);
    } else {
    printf("%d is not a magic number\n", num);
    }

    return 0;
    }

    ReplyDelete
  14. Example: 1729

    Find the sum of digits of the given number.(1 + 7 + 2 + 9 => 19)
    Reverse of digit sum output. Reverse of 19 is 91
    Find the product of digit sum and the reverse of digit sum.(19 X 91 = 1729)
    If the product value and the given input are same, then the given number is a magic number.(19 X 91 <=> 1729)
    So, 1729 is a magic number.

    ReplyDelete
  15. The logics are wrong
    eg-73
    7+3=10
    1+0=1
    the final result should be one digit number
    and should be 1

    ReplyDelete