Happy Number
Write a program to accept a number and check whether it is a happy number or not.
Write a program to accept a number and check whether it is a happy number or not.
A happy number is a number in which the eventual sum of the square of the digits of the number is equal to 1.
for example:
28-->2^2+8^2=4+64=68
68-->6^2+8^2=36+64=100
100-->1^2+0^2+0^2=1+0+0=1
so 28 is a happy number.
import java.io.*;
class Happy
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int num=Integer.parseInt(br.readLine());
int sum=0;
double a=0;
while(num>=0)
{
a=num%10;
a=Math.pow(a,2);
sum+=a;
num=num/10;
if(num==0)
{
if(sum<10)
break;
else
{
num=sum;
sum=0;
continue;
}
}
}
if(sum==1)
System.out.println("It is a happy number");
else
System.out.println("It is a happy number");
}
}Related Posts
Continue learning with these related articles

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

Boolean Algebra for ISC Class 12: Laws, Canonical SOP and POS, and Simplification
ISC Class 12 Computer Science notes on Boolean algebra: all the laws and theorems, duality, De Morgan's laws, minterms and maxterms, converting to canonical SOP and POS, and step-by-step algebraic simplification with solved examples.