Computer Science, asked by sacpri, 1 year ago

write a program to check whether a no. is happy no. or not

Answers

Answered by MacTavish343
1
¤¤HELLO¤¤

import java.io.*;

class Happy

{

static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int n;

 

Happy()

  {

   n=0;

  }

 

void getnum(int nn)

 {

  n=nn;

 }

 

int sum_sq_digits(int x)

 {

  if(x==0)

    return 0;

  else

   {

    int d=x%10;

    return (d*d+ sum_sq_digits(x/10));

   }

 }

 

void ishappy()

{

int a=sum_sq_digits(n);

while(a>9)

{

a=sum_sq_digits(a);

}

if(a==1)

   System.out.print(n+" is a Happy Number");

else

   System.out.print(n+" is Not a Happy Number");

}

 

public static void main(String args[])throws IOException

  {

   Happy ob=new Happy();

System.out.print("Enter any number: ");

   int b=Integer.parseInt(br.readLine());

   ob.getnum(b); 

   ob.ishappy();

  }

}

¥¥ HOPE THIS HELPS YOU ¥¥
Similar questions