Advertisement
noob339

Hailstone

Dec 13th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <math.h>
  4.  
  5. void printArr(int arr[], int size);
  6.  
  7. int main(void)
  8. {
  9.     int num = get_int("type in an integer: ");
  10.    
  11.     int num2 = num;
  12.    
  13.     int count = 0;
  14.    
  15.     while (num != 1)
  16.     {
  17.         if (num % 2 == 0)
  18.         {
  19.             num /= 2;
  20.             count++;
  21.             //printf("%i\n", num);
  22.         }
  23.         else if (num % 2 != 0)
  24.         {
  25.             num = (num * 3) + 1;
  26.             count++;
  27.             //printf("%i\n", num);
  28.         }
  29.     }
  30.    
  31.     int arr[count];
  32.    
  33.     printf("%i", count);
  34.    
  35.     int i = 0;
  36.    
  37.     while (num2 != 1)
  38.     {
  39.         if (num2 % 2 == 0)
  40.         {
  41.             num2 /= 2;
  42.             arr[i] = num2;
  43.         }
  44.         else if (num2 % 2 != 0)
  45.         {
  46.             num2 = (num2 * 3) + 1;
  47.             arr[i] = num2;
  48.         }
  49.     i++;
  50.     }
  51.  
  52.     printArr(arr, count);
  53.  
  54. }
  55.  
  56. void printArr(int arr[], int size)
  57. {
  58.     for (int i = 0; i < size; i++)
  59.     {
  60.         printf("%i\n", arr[i]);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement