Monday, May 24, 2010

Can you tell me something related to programming in c++ ?

// Program to input a number and check if it is a Magic Number


#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


main()


{ clrscr();


int num,num1,i,j,k,flag=0,n,sum=0,c=0;


cout%26lt;%26lt;"enter any no.";


cin%26gt;%26gt;num;


num1=num;


while(flag==0)


{


while(num!=0)


{ n=num%10;


sum=sum+n;


num=num/10;


c=c+1; }


if (c==1)


{


if (sum==1) cout%26lt;%26lt;num1%26lt;%26lt;" is a magic number";


else


cout%26lt;%26lt;num1%26lt;%26lt;" is not a magic number";


flag=1;}


num=sum;


sum=0;


c=0;


}


getch(); }


hi! this is a sample program of c++, there are no errors in it.


what i don't understand in this is MAGIC NUMBERS. WHAT ACTUALLY IS THIS MAGIC NUMBER?

Can you tell me something related to programming in c++ ?
What exactly are you trying to do? Is this program for a class? If so, then provide the specifications for the assignment. Beyond that, the only magic number is 1.





n = num % 10





returns the remainder of num divided by 10, so anything other than a number that ends in 1 will fail the conditional statement





if(sum==1) ...





Also, any number equal to or higher than 10 will cause the WHILE loop to cycle through two or more times. This causes C to equal 2 or higher, depending on the number of cycles. This causes another conditional statement to fail too.





if(c==1)





If this program is supposed to work some other way, as mentioned I'll need specifications before I can help you any further.
Reply:Here is the definition of the MAGIC NUMBER:


http://en.wikipedia.org/wiki/Magic_numbe...

rosemary

No comments:

Post a Comment