Skip to main content

Happy Number Solution | Recursion

 /**

 * Write a Java program to find and print the Happy numbers between m and n.

Happy number: Starting with any positive integer, replace the number by the sum of the squares of its digits, 

and repeat the process until the number equals 1, or it loops endlessly in a cycle which does not include 1.

Example: 19 is a happy number

1^2 + 9^2=82

8^2 + 2^2=68

6^2 + 8^2=100

1^2 + 0^2 + 02=1


The first few happy numbers are 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, ...

 */

import java.util.*;

public class HappyNum

{

    public static int digitSq(int x) // recursive function to find square of digits of the number

    {

        if(x<10)

        return x*x;

        else

        return (x%10)*(x%10) + digitSq(x/10);

    } // end of digitSq()

    

    public static boolean isHappy(int num, int i)  // recursive function to check whether the no. is happy or not

    {

        //System.out.println(num);

        if(num==1)

        return true;

        else if(i==15)

        return false;

        else

        return isHappy( digitSq(num), i+1);

    } // end of isHappy()

    

    public static void main()

    {

        Scanner sc=new Scanner(System.in);

        System.out.print("\nEnter lower limit : ");

        int m=sc.nextInt();

        System.out.print("\nEnter upper limit : ");

        int n=sc.nextInt();

        System.out.println("The Happy numbers are : ");

        for(int i=m;i<=n;i++)

        {

            if( isHappy(i, 0) )

            {

                System.out.print(i+"    ");

            }

        }

    } // end of main()

} // end of class

Comments

Popular posts from this blog

Program to perform to certain tasks using numbers | C programming

     1.          Q.1      WAP in C/ C++/ java to perform the following tasks : a.       Accept two numbers from the user( a and b). b.       Print the numbers. c.        Print the smallest number. d.       Calculate their :                                i.       Summation (a+b)                               ii.       Subtract them (a-b)                              iii.       Multiply them (a*b)                              iv.  ...

Neon Number | C programming

 Q.8     WAP in C/ C++/ java to check whether the number is a Neon number or not. Input: 9 Output: Neon Number Explanation: the square is 9*9 = 81 and the sum of the digits of the square ( 8+1) is 9. Solution :  #include <stdio.h> int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      return   sum ; } int   main () {      int   n ;         printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     { ...

Special Number | C programming

 Q.9    WAP in C/ C++/ java to check whether the number is a Special Number or not. Definition: If the sum of the factorial of digits of a number (N) is equal to the number itself, the number (N) is called a  special  number. INPUT : 145     The digits of the number are: 1, 4, 5 Factorial of digits: 1! = 1 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Sum of factorial of digits = 1 + 24 + 120 = 145             Hence, the given number 145 is a special number. Solution :  #include   <stdio.h> int   factorial ( int  x)  // function to return factorial of the number {      int   f   =   1 ;      for  ( int   i   =   1 ;  i   <=  x;  i ++ )     {     ...