Saturday 4 March 2017

Magic Number


Program for check whether a number is Magic or Not

A Magic number said to Magic on the behalf of following rule
  • Sum of each digit (1729=1+7+2+9=19)
  • Reverse obtain sum of digit i.e. (19=91)
  • Find product of sum of digit and reverse sum of digit i.e. (19*91=1729)
  • If result is equal to input number then it is Magic number

/* *****************************************
     Girfa : Student Help
     Magic Number Program
     for more program visit : http://girfahelp.blogspot.in/p/c-language-assignment.html
     *****************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
     int n1,n2,i,sd=0,sr=0,j;
     clrscr();
     printf("Enter Number>> ");
     scanf("%d",&n1);
     n2=n1;
     /* sum of each digit */
     while(n1>0)
     {
           sd=sd+n1%10;
           n1=n1/10;
     }
     /* Reverse sum of each digit */
     n1=sd;
     while(n1>0)
     {
           sr=sr*10+n1%10;
           n1=n1/10;
     }
     /* Product of sum of digit and reverse sum of digit */
     n1=sd*sr;
     if(n1==n2)
           printf("\n\tMagic Number");
     else
           printf("\n\tNot a Magic Number");
     getch();
}


No comments:

Post a Comment