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