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();
}

2 comments:

Question: What is flag used for??

Can anyone explain fast please.

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More