Skip to main content

Happy number in java, program to check happy number

Happy number is similar to magic number, but instead of obtaining sum of the digits we obtain sum of square of the digits.


EXAMPLE-1
19 =1²+9²=82
82=8²+2²=68
68=6²+8²=100
100=1²+0²+0²=1

EXAMPLE-2
7=7²=49
49=4²+9²=97
97=9²+7²=130
130=1²+3²+0²=10
10=1²+0²=1

EXAMPLE-3
13=1²+3²=10
10=1²+0²=1

A java program on happy number can be made by extracting digits of the number then adding the square of the digits. If the sum result to 1 then it is a happy number and if it not results to 1 then if the sum is greater than 10 then it digits are re-extracted and then again added it's square of digits.


JAVA CODE-

import java.util.*;
public class happy
{
int n;
happy()
{
    n=0;
}
void getNum(int nx)
{
  n=nx;  
}
int sum_sq_digits(int d)
{
    int sum=0;
    while(d>0)
    {
        sum=sum+((d%10)*(d%10));
        d=d/10;
    }
    int s;int c=0;
    while(sum>9)
    {
        s=sum;
        while(s>0)
        {
            c=c+(s%10);
            s=s/10;
        }
        sum=c;
    }
    return sum;
}
void isHappy()
{
    if(sum_sq_digits(n)==1)
    System.out.println("it is a happy number");
    else
    System.out.println("it is not a happy number");
}
public static void main(String[] arg)
{
    Scanner sc=new Scanner(System.in);
    System.out.println("enter a number");
    int x=sc.nextInt();
    happy obj=new happy();
    obj.getNum(x);
    obj.isHappy();
}
}


Output-

enter a number 
19
It is a happy number

SIMILAR JAVA CODE-

If you find helpful please share

Comments

Popular posts from this blog

Java program for prime numbers

A prime number is the number which is is  divisible by itself and not by other numbers. A program to check prime number can be made by using for and while loop combination . This article contains java program for finding prime number ,write a java program for prime number between 1- 100 ,simple program to find prime factorial of a number in java. EXAMPLE 1 - program to check whether a number is prime number  or not. import java.util.*; public class pr { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("enter number"); int num=sc.nextInt(); int d=0; for(int i=2;i<=num;i++) { if(num%i==0) d++; } if(d==0) System.out.println("it is a prime number"); else System.out.println("it is not a prime number"); } } Output enter number 23 it is a p...

Palindrome number in java

A number is said to be palindrome if it reads both forward and backwards same. Example- 175571 454 A program for palindrome number can be made by simply extracting digits of the number in reverse order and adding them with their place value and then checking is it same as the original number or not. To extract a digit in revers order and adding with place value we simply follow the below code. while(n>0) { int d=n%10; rev=(rev*10)+d; n=n/10; } To explain this let us take an example 657 Then  For first iteration =  n=657                                     d=657%10=7                                     rev=(0*10)+7=7 For second iteration = n=65                                         ...

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 m...