Skip to content

Commit

Permalink
Merge pull request sujana-kamasany#127 from PractiseRepo/main
Browse files Browse the repository at this point in the history
added a program to find the magic number
  • Loading branch information
sujana-kamasany authored Nov 1, 2022
2 parents d65d9b7 + e4ec679 commit 0ed7ac2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .classpath
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>
17 changes: 17 additions & 0 deletions .project
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>
42 changes: 42 additions & 0 deletions MagicNumber.java
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.");
}
}
}

0 comments on commit 0ed7ac2

Please sign in to comment.