Skip to content
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

Add Happy Number Detector #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions HappyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.Scanner;

/*
* A happy number is a number that, when you repeatedly replace it with the sum of the square of its digits,
* eventually reaches 1.
* Example: 19 is a happy number because 1^2 + 9^2 = 82, and 8^2 + 2^2 = 68, and so on until it reaches 1.
*/
public class HappyNumber {
public static void main(String[] args) {
// Initialize the scanner class to take input
Scanner scanner = new Scanner(System.in);

// Prompt the user for input
System.out.print("Enter a number to check if it's a Happy Number: ");

// Take input
int number = scanner.nextInt();

// Check if the number is a Happy Number and print the desired message
if (isHappy(number)) {
System.out.println(number + " is a Happy Number.");
} else {
System.out.println(number + " is not a Happy Number.");
}
}

/**
* Checks if a given integer is a Happy Number.
*
* @param number the number to check.
* @return true if the input number is a Happy Number, false otherwise.
*/
public static boolean isHappy(int number) {
// Initialize the sum of squared digits to 0.
int sum = 0;

// Iterate until n becomes 0.
while (number > 0) {
// Extract the last digit.
int temp = number % 10;

// Square and add the digit to the sum.
sum += (int) Math.pow(temp, 2);

// Remove the last digit by integer division.
number /= 10;
}

// Check if the sum of squared digits is 1, indicating a Happy Number.
if (sum == 1) {
return true;
} else if (sum == 4) {
// If the sum becomes 4, it will never reach 1, so it's not a Happy Number.
return false;
} else {
// Recursively call isHappy with the sum until we reach either 1 or 4.
return isHappy(sum);
}
}
}