A Magic Number is a number in which the eventual sum of digits of the number is equal to 1.
For example, 172=1+7+2=10
10=1+0=1
Then 172 is a Magic Number.
Design a class Magic to check if a given number is a magic number. Some of the members of the class
are given below:
Class Name : Magic
Data members/instance variables:
n : stores the number
Member functions :
Magic() : constructor to assign 0 to n
void getnum(int nn) : to assign the parameter value to the number, n=nn
int Sum_of_digits(int) : returns the sum of the digits of a number
void ismagic() : checks if the given number is a magic number by calling the
function
Specify the class Magic giving details of the constructor, void getnum(int), int Sum_of_digits(int) and
void ismagic().Define the main () function to create an object and call the functions accordingly to
enable the task.
import [Link].*;
class Magic
{
int n;
Magic()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_of_digits(int s)
{
int d,sum=0,num=s;
while(num>0)
{
d=num%10;
sum=sum+d;
num=num/10;
}
return sum;
}
void ismagic()
{
while (n>9)
{
int su=sum_of_digits(n);
n= su;
su = 0;
}
if (n == 1)
{
[Link]("The given number is a magic number.");
}
else
{
[Link]("The given number is not a magic number.");
}
}
public static void main()
{
Scanner in =new Scanner([Link]);
int no;
[Link]("Enter a number");
no=[Link]();
Magic m=new Magic();
[Link](no);
[Link]();
}
}