-
Notifications
You must be signed in to change notification settings - Fork 18.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Happy numbers problem using thread #2832
Comments
The code is difficult to understand without indentation |
Yes sorry. Here the code with indentation : And here the explication about what is a happy number : |
This issue is called "Happy numbers problem using thread #2832", but I don't see any multithreading in the code. Could you elaborate as to what you want to do? |
Feel free to add any of the discussed algorithms in a PR |
import java.util.;
public class HappyNumSeqTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
while(n != 1 && !isSad(n)){
System.out.print(n+" ");
n = sumSquares(n);
}
if(n == 1) System.out.println("1 Happy number");
else
System.out.println("Sad number");
}
static int sumSquares(int n){
int s = 0;
while(n > 0){
int r = n%10;
s = s + rr;
n = n/10;
}
return s;
}
static boolean isSad(int n){
int cycleNums[] = {4,16,20,37,58,145};
boolean found = false;
int j = 0;
while(j<cycleNums.length && !found)
if(cycleNums[j] == n)
found = true;
else
j++;
return found;
}
}
Example of output :
Enter number: 50
50 25 29 85 89 Sad number
Or
Enter number: 19
19 82 68 100 1 Happy number
The text was updated successfully, but these errors were encountered: