Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.36 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int n, sum, d;
  6.  
  7.     printf("Enter a number to check if it's happy: ");
  8.     scanf("%d", &n);
  9.  
  10.     while (n > 0) {
  11.         sum = 0;
  12.         while (n > 0) {
  13.             d = (n%10);
  14.             n/=10;
  15.             sum += d*d;
  16.         }
  17.         n = sum;
  18.  
  19.         if ( n < 10 )
  20.             break;
  21.     }
  22.  
  23.     if (n == 1)
  24.         printf("Happy number!");
  25.     else
  26.         printf("Not a happy number.");
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement