English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

// Program to input a number and check if it is a Magic Number
#include
#include
main()
{ clrscr();
int num,num1,i,j,k,flag=0,n,sum=0,c=0;
cout<<"enter any no.";
cin>>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< else
cout< 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?

2006-12-26 21:51:45 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

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.

2006-12-26 22:56:25 · answer #1 · answered by Kookiemon 6 · 0 0

Here is the definition of the MAGIC NUMBER:
http://en.wikipedia.org/wiki/Magic_number_(programming)

2006-12-27 10:45:03 · answer #2 · answered by gtopala 4 · 0 0

fedest.com, questions and answers