Two Pointer(java);

In Two pointer topic today i have completed the task of finding the happy number in java.

//happy number

public class happy_num {
static boolean happy(int a){

if(a==1){
return true;
}
else{
return isHappy(a);
}
}…


This content originally appeared on DEV Community and was authored by Vasanth S

In Two pointer topic today i have completed the task of finding the happy number in java.

//happy number

public class happy_num {
static boolean happy(int a){

    if(a==1){
        return true;
    }
    else{
        return isHappy(a);
    }
}
public static boolean isHappy(int n) {
    if(n<7 && n>1){//2 
        return false;
    }
    int result=0;
    while(n>0){
        int l=n%10;//rem 
        result+=l*l;//82
        n=n/10;

    }
    if(result==1){
        return true;
    }
    else {
        return happy(result);
    }

}

public static void main(String args[]){
int num=5;
boolean res =isHappy(num);
System.out.println(res);
}

In the above program to find the logic and use recursion technique;


This content originally appeared on DEV Community and was authored by Vasanth S


Print Share Comment Cite Upload Translate Updates
APA

Vasanth S | Sciencx (2025-02-04T18:08:00+00:00) Two Pointer(java);. Retrieved from https://www.scien.cx/2025/02/04/two-pointerjava/

MLA
" » Two Pointer(java);." Vasanth S | Sciencx - Tuesday February 4, 2025, https://www.scien.cx/2025/02/04/two-pointerjava/
HARVARD
Vasanth S | Sciencx Tuesday February 4, 2025 » Two Pointer(java);., viewed ,<https://www.scien.cx/2025/02/04/two-pointerjava/>
VANCOUVER
Vasanth S | Sciencx - » Two Pointer(java);. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/04/two-pointerjava/
CHICAGO
" » Two Pointer(java);." Vasanth S | Sciencx - Accessed . https://www.scien.cx/2025/02/04/two-pointerjava/
IEEE
" » Two Pointer(java);." Vasanth S | Sciencx [Online]. Available: https://www.scien.cx/2025/02/04/two-pointerjava/. [Accessed: ]
rf:citation
» Two Pointer(java); | Vasanth S | Sciencx | https://www.scien.cx/2025/02/04/two-pointerjava/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.