0% found this document useful (0 votes)
53 views2 pages

Class Based Magic Number

The document describes a Java class named 'Magic' that checks if a given number is a Magic Number, defined as a number whose digits eventually sum to 1. It includes methods for initializing the number, calculating the sum of digits, and determining if the number is a Magic Number. The main function prompts the user for input, creates an instance of the Magic class, and calls the appropriate methods to perform the check.

Uploaded by

shivamlayek828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views2 pages

Class Based Magic Number

The document describes a Java class named 'Magic' that checks if a given number is a Magic Number, defined as a number whose digits eventually sum to 1. It includes methods for initializing the number, calculating the sum of digits, and determining if the number is a Magic Number. The main function prompts the user for input, creates an instance of the Magic class, and calls the appropriate methods to perform the check.

Uploaded by

shivamlayek828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like