Happy Number (C)


  • 0
    P
    bool isHappy(int n) {
      int sum;
      for ( int i=0; i<20; i++) {
      	sum = 0;
      	while( n != 0) {
      		sum = sum + (n%10)*(n%10);
      		n = n/10;
      	}
      	if (sum == 1)
      		return 1;
      	n = sum;
      }
      return 0;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.