-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a program to find the magic number
- Loading branch information
Rohini KP
authored and
Rohini KP
committed
Oct 20, 2022
1 parent
7a2dd48
commit e4ec679
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>javacodes</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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."); | ||
} | ||
} | ||
} |