dmilicev

when_World_War_IV_begins.c

Mar 7th, 2022 (edited)
431
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     when_World_War_IV_begins.c
  4.  
  5. Beginning of World War I:
  6. DD.MM.YYYY.
  7. 28.7.1914.
  8. 2+8+7+1+9+1+4 = 32 -> 3+2 = 5
  9.  
  10. Beginning of World War II:
  11. DD.MM.YYYY.
  12. 01.9.1939.
  13. 1+9+1+9+3+9 = 32 -> 3+2 = 5
  14.  
  15. Beginning of World War III:
  16. (Beginning of a special action in Ukraine)
  17. DD.MM.YYYY.
  18. 24.2.2022.
  19. 2+4+2+2+0+2+2 = 14 -> 1+4 = 5
  20.  
  21. Beginning of World War IV ?
  22.  
  23. Critical dates are
  24.  
  25. 07.01.2022. 16.01.2022. 25.01.2022.
  26. 06.02.2022. 15.02.2022. 24.02.2022.
  27. 05.03.2022. 14.03.2022. 23.03.2022.
  28. 04.04.2022. 13.04.2022. 22.04.2022.
  29. 03.05.2022. 12.05.2022. 21.05.2022. 30.05.2022.
  30. 02.06.2022. 11.06.2022. 20.06.2022. 29.06.2022.
  31. 01.07.2022. 10.07.2022. 19.07.2022. 28.07.2022.
  32. 09.08.2022. 18.08.2022. 27.08.2022.
  33. 08.09.2022. 17.09.2022. 26.09.2022.
  34. 07.10.2022. 16.10.2022. 25.10.2022.
  35. 06.11.2022. 15.11.2022. 24.11.2022.
  36. 05.12.2022. 14.12.2022. 23.12.2022.
  37. 06.01.2023. 15.01.2023. 24.01.2023.
  38.  
  39.  
  40.     You can find all my C programs at Dragan Milicev's pastebin:
  41.  
  42.     https://pastebin.com/u/dmilicev
  43.  
  44. */
  45.  
  46. #include <stdio.h>
  47.  
  48. // C function to check if a given year is leap year or not.
  49. // Returns 1 if year is leap, otherwise returns 0.
  50. // Rule 1. Every fourth year is a leap.
  51. // Rule 2. The exception to Rule 1 is that every hundredth year is not a leap.
  52. // Rule 3. The exception to Rule 2 is that every 400th year is a leap.
  53. int leapYear(int year){
  54.     // Return true if year is a multiple of 4 and not multiple of 100.
  55.     // OR year is multiple of 400.
  56.     if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
  57.         return 1;
  58.     else
  59.         return 0;
  60. }
  61.  
  62. // extract digits from integer into array digits[]
  63. // returns the number of extracted digits
  64. int extract_digits_from_integer( int number, int digits[8] ){
  65.     int i, j, number_of_digits = 0, mem;
  66.  
  67.     while( number != 0 )    {
  68.         digits[number_of_digits++] = number % 10;   // fill array digits[]
  69.         number = number / 10;
  70.     }
  71.  
  72.     // reverse first number_of_digits elements of array digits[]
  73.     for ( i=0, j=number_of_digits-1; i<number_of_digits/2; i++, j-- )   {
  74.         mem = digits[i];
  75.         digits[i] = digits[j];
  76.         digits[j] = mem;
  77.     }
  78.  
  79.     return( number_of_digits );
  80. } // extract_digits_from_integer
  81.  
  82. // finds the sum of the digits of a integer num until the sum is reduced to a single digit
  83. int reduce_sum_of_digits_to_a_single_digit( int num ){
  84.     int sum, rem;
  85.     while(num / 10 != 0){
  86.         sum = 0;
  87.         while(num != 0){
  88.             rem = num % 10;  // get the last digit of num
  89.             sum += rem;      // add rem to sum
  90.             num = num / 10;  // remove the last digit from num
  91.         }
  92.         num = sum;
  93.     }
  94.     return sum;
  95. } // reduce_sum_of_digits_to_a_single_digit()
  96.  
  97.  
  98. int main(void){
  99.     int lastDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  100.     int digits_of_the_day[2], digits_of_the_month[2], digits_of_the_year[4];
  101.     int number_of_day_digits, number_of_month_digits, number_of_year_digits;
  102.     int firstYear=2022, lastYear=2050, d, m, y=firstYear;
  103.     int i, counter_of_possible_dates=0, sum_of_date_digits, sum, rem;
  104.  
  105.     for(y=firstYear;y<=lastYear;y++){       // years
  106.  
  107.         if( leapYear(y) )   // calculation of the number of days in February
  108.             lastDay[1]=29;
  109.         else
  110.             lastDay[1]=28;
  111.  
  112.         for(m=1;m<=12;m++){                 // months
  113.  
  114.             for(d=1;d<=lastDay[m-1];d++){   // days
  115.                 sum_of_date_digits=0;
  116.                 sum=0;
  117.  
  118.                 number_of_day_digits = extract_digits_from_integer(d,digits_of_the_day);
  119.                 number_of_month_digits = extract_digits_from_integer(m,digits_of_the_month);
  120.                 number_of_year_digits = extract_digits_from_integer(y,digits_of_the_year);
  121.  
  122.                 for(i=0;i<number_of_day_digits;i++)
  123.                     sum_of_date_digits += digits_of_the_day[i];
  124.  
  125.                 for(i=0;i<number_of_month_digits;i++)
  126.                     sum_of_date_digits += digits_of_the_month[i];
  127.  
  128.                 for(i=0;i<number_of_year_digits;i++)
  129.                     sum_of_date_digits += digits_of_the_year[i];
  130.  
  131.                 if ( reduce_sum_of_digits_to_a_single_digit(sum_of_date_digits) == 5 ){
  132.                     printf("%02d.%02d.%04d. ", d, m, y);
  133.                     counter_of_possible_dates++;
  134.  
  135.                     if(counter_of_possible_dates%6==0)
  136.                         printf("\n");
  137.                 }
  138.  
  139.             } // days
  140.  
  141.         } // months
  142.  
  143.     } // years
  144.  
  145.  
  146.     printf("\n\n From %d to %d there are %d possible dates. \n", firstYear, lastYear, counter_of_possible_dates);
  147.  
  148.     return 0;
  149. } // main()
  150.  
RAW Paste Data Copied