write a program in Java
to input number user ( by BufferedReader ) and print whether they are Happy number or not.
Answers
Answer:
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
// Utility method to return sum of square of
// digit of n
static int numSquareSum(int n)
{
int squareSum = 0;
while (n!= 0)
{
squareSum += (n % 10) * (n % 10);
n /= 10;
}
return squareSum;
}
// method return true if n is Happy number
static boolean isHappynumber(int n)
{
int slow, fast;
// initialize slow and fast by n
slow = fast = n;
do
{
// move slow number
// by one iteration
slow = numSquareSum(slow);
// move fast number
// by two iteration
fast = numSquareSum(numSquareSum(fast));
}
while (slow != fast);
// if both number meet at 1,
// then return true
return (slow == 1);
}
// Driver code to test above methods
public static void main(String[] args)
{
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
String str=inp.readLine();
int n= Integer.parseInt(str);
if (isHappynumber(n))
System.out.println(n +
" is a Happy number");
else
System.out.println(n +
" is not a Happy number");
}
}
Explanation:
Please Mark me as Brainiest...
Example
13= + =10
10= + =1
Like this processing ends with one then it is happy number otherwise not.