forked from sujana-kamasany/javacodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagicNumber.java
42 lines (40 loc) · 993 Bytes
/
MagicNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MagicNumber {
public static void main(String args[])
{
magic();
}
public void magic()
{
int n, remainder = 1, number, sum = 0;
//creating a constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number you want to check: ");
//reading an integer form the user
n = sc.nextInt();
//assigning the entered number in the variable num
number = n;
//outer while loop
while (number > 9) //while(number > 0 || sum > 9)
{
//inner while loop
while (number > 0)
{
//determines the remainder
remainder = number % 10;
sum = sum + remainder;
//divides the number by 10 and removes the last digit of the number
number = number / 10;
}
number = sum;
sum = 0;
}
if (number == 1)
{
System.out.println("The given number is a magic number.");
}
else
{
System.out.println("The given number is not a magic number.");
}
}
}