Two Pointer(java);

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);
    }
}
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;

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
ziff-davis’s-study-reveals-that-llms-favor-high-da-websites

Ziff Davis’s Study Reveals That LLMs Favor High DA Websites

Next Post
understanding-the-role-of-machine-vision-and-ethernet-switches-in-gige-vision-imaging-systems

Understanding the Role of Machine Vision and Ethernet Switches in GigE Vision Imaging Systems

Related Posts