u

Thursday 7 June 2012

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

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


#include<iostream.h>
#include<conio.h>
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<<num1<<" is a magic number";
else
cout<<num1<<" is not a magic number";
flag=1;}
num=sum;
sum=0;
c=0;
}

getch();
}

4 comments:

Question: What is flag used for??

Can anyone explain fast please.

Flag is just a variable used to check equality of the magic number with the number given by user

i think it is lenghty ...
and what is variable c.

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More