0% found this document useful (0 votes)
168 views6 pages

Program I:: Int N

The document describes a program to check if a number is a mystic number. A mystic number is one where the sum of its digits is equal to 3. The program includes a Mystic class with methods to input a number, calculate the sum of its digits, and check if it is a mystic number. Main method creates a Mystic object, takes user input, calls the methods to check and display the result.

Uploaded by

monalisa
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)
168 views6 pages

Program I:: Int N

The document describes a program to check if a number is a mystic number. A mystic number is one where the sum of its digits is equal to 3. The program includes a Mystic class with methods to input a number, calculate the sum of its digits, and check if it is a mystic number. Main method creates a Mystic object, takes user input, calls the methods to check and display the result.

Uploaded by

monalisa
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
You are on page 1/ 6

PROGRAM I:

A MYSTIC NUMBER IS A NUMBER IN WHICH THE


EVENTUAL SUM OF THE DIGITS IS EQUAL TO
THREE.
Example : 984 is a mystic number because
. 9 + 8+4 =21=2+1=3
DESIGN A CLASS TO INPUT A NUMBER AND FIND
WHETHER IT IS A MYSTIC NUMBER OR NOT.
INCLUDE THE FOLLOWING MEMBERS:
INSTANCE VARIABLES:
 int n
INSTANCE METHODS:
 public Mystic()-Default constructor to initialize n to 0
 public void fillNum(int num) – to assign value to n
 public intsumOfDigits() – to find the sum of the digits
of the number
 public boolean isMystic() – to check whether the
number is mystic or not and display appropriate
message.
Write a main method the create an object and call the
above methods accordingly.
SOURCE CODE:
OUTPUT:
VARIABLE LISTING:
SCOPE : public class Mystic
VARIABLE VARIABLE PURPOSE
NAME TYPE
n int To store the inputted number

SCOPE: public void fillNum( )


VARIABLE VARIABLE PURPOSE
NAME TYPE
num int Formal parameter; to store the
value to be assigned to n

SCOPE: public int sumOfDigits( )


VARIABLE VARIABLE PURPOSE
NAME TYPE
copy int To store a copy of n on which
the operations would be carried
out
sum int To store the sum of the digits

SCOPE: public boolean isMystic( )


VARIABLE VARIABLE PURPOSE
NAME TYPE
flag boolean To store the result after
condition checking
SCOPE: public static void main (String [] args)
VARIABLE VARIABLE PURPOSE
NAME TYPE
obj Mystic Refers to class object; used to
call instance methods
scan Scanner Refers to Scanner object used
to take keyboard input
num int To store the number inputted
by the user

ALGORITHM:
public static void main(String [] args):
1. Start
2. Scanner object is created
3. object of the class Mystic is created
4. num is taken as input
5. if num<0(num is negative);
then, “Wrong input” is printed
Go to 10.
6. fillNum(num) method is called
7. if value returned by isMystic() method = true,
then, it is displayed as a mystic number
8. else, it is displayed as not a mystic number.
9. endif
10. end
public void fillNum(int num):
1. Start
2. The parameter num is assigned to the instance variable n
3. End
public boolean isMystic():
1. Start
2. boolean variable flag is initialized to false
3.if 3= value returned by the method sumOfDigits( ),
then, flag is set to true
4. value of flag is returned to calling method
5. End
public int sumOfDigits( ):
1. Start
2. copy is initialized to n and sum to 0.
3. if copy>0,
then repeat steps 4 to 5.
4. the last digit of copy is added to the previous sum.
5. the value of copy is divided by 10
6. go to 3
7. if sum >=10,
then repeat steps 8 to 10.
8. copy is initialized to sum and sum to 0.
9. repeat steps 3 to 6.
10. go to 7
11.the value of sum is returned.
12. End.

You might also like