"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, January 7, 2010

Magic No.

/* calculate the sum of all digits of an integer into single digit. If the sum is equal to 1 , then it's a magic no.
 10 -> 1+0 = 1 Magic no.
901 -> 9+0+1 = 10 -> 1+0 =1 Magic No.
 203 -> 2+0+3 = 5 Not  */


#include<iostream.h>
#include<conio.h>
void main()
{
int num, n, s = 0, r, i;

cout<<"\n Enter an positive  interger :";
cin>>num;

n = num;

while(n>9)
{
for(i = n; i >0; i = n/10)
{
r = n % 10;
s = s+r;
}
n = s;
s = 0;
}

if(n = = 1)
cout<<"\n"<< num <<" is a MAGIC NO.";
else
cout<<"\n"<< num <<"is not a MAGIC NO>";

getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.