fork download
  1. #include <iostream>
  2. #include <math.h>
  3. /*
  4.  Prayers calculator start
  5. */
  6.  
  7. //convert Degree to Radian
  8. double degToRad(double degree)
  9. {
  10. return ((3.1415926 / 180) * degree);
  11. }
  12.  
  13. //convert Radian to Degree
  14. double radToDeg(double radian)
  15. {
  16. return (radian * (180/3.1415926));
  17. }
  18.  
  19. //make sure a value is between 0 and 360
  20. double moreLess360(double value)
  21. {
  22. while(value > 360 || value < 0)
  23. {
  24. if(value > 360)
  25. value -= 360;
  26.  
  27. else if (value <0)
  28. value += 360;
  29. }
  30.  
  31. return value;
  32. }
  33.  
  34. //make sure a value is between 0 and 24
  35. double moreLess24(double value)
  36. {
  37. while(value > 24 || value < 0)
  38. {
  39. if(value > 24)
  40. value -= 24;
  41.  
  42. else if (value <0)
  43. value += 24;
  44. }
  45.  
  46. return value;
  47. }
  48.  
  49. //convert the double number to Hours and Minutes
  50. void doubleToHrMin(double number, int &hours, int &minutes)
  51. {
  52. hours = floor(moreLess24(number));
  53. minutes = floor(moreLess24(number - hours) * 60);
  54. }
  55.  
  56. void calcPrayerTimes(int year, int month, int day,
  57. double longitude, double latitude, int timeZone,
  58. double fajrTwilight, double ishaTwilight,
  59. double &fajrTime, double &sunRiseTime, double &zuhrTime,
  60. double &asrTime, double &maghribTime, double &ishaTime)
  61. {
  62. double D = (367 * year) - ((year + (int)((month + 9) / 12)) * 7 / 4) + (((int)(275 * month / 9)) + day - 730531.5);
  63.  
  64. double L = 280.461 + 0.9856474 * D;
  65. L = moreLess360(L);
  66.  
  67. double M = 357.528 + (0.9856003) * D;
  68. M = moreLess360(M);
  69.  
  70. double Lambda = L + 1.915 * sin(degToRad(M)) + 0.02 * sin(degToRad(2 * M));
  71. Lambda = moreLess360(Lambda);
  72.  
  73. double Obliquity = 23.439 - 0.0000004 * D;
  74. double Alpha = radToDeg(atan((cos(degToRad(Obliquity)) * tan(degToRad(Lambda)))));
  75. Alpha = moreLess360(Alpha);
  76.  
  77. Alpha = Alpha - (360 * (int)(Alpha / 360));
  78. Alpha = Alpha + 90 * (floor(Lambda / 90) - floor(Alpha / 90));
  79.  
  80. double ST = 100.46 + 0.985647352 * D;
  81. double Dec = radToDeg(asin(sin(degToRad(Obliquity)) * sin(degToRad(Lambda))));
  82. double Durinal_Arc = radToDeg(acos((sin(degToRad(-0.8333)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  83.  
  84. double Noon = Alpha - ST;
  85. Noon = moreLess360(Noon);
  86.  
  87.  
  88. double UT_Noon = Noon - longitude;
  89.  
  90.  
  91. ////////////////////////////////////////////
  92. // Calculating Prayer Times Arcs & Times //
  93. //////////////////////////////////////////
  94.  
  95. // 2) Zuhr Time [Local noon]
  96. zuhrTime = UT_Noon / 15 + timeZone;
  97.  
  98. // Asr Hanafi
  99. //double Asr_Alt =radToDeg(atan(2 + tan(degToRad(abs(latitude - Dec)))));
  100.  
  101. // Asr Shafii
  102. double Asr_Alt = radToDeg(atan(1 + tan(degToRad(abs(latitude - Dec)))));
  103. double Asr_Arc = radToDeg(acos((sin(degToRad(90 - Asr_Alt)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  104. Asr_Arc = Asr_Arc / 15;
  105. // 3) Asr Time
  106. asrTime = zuhrTime + Asr_Arc;
  107.  
  108. // 1) Shorouq Time
  109. sunRiseTime = zuhrTime - (Durinal_Arc / 15);
  110.  
  111. // 4) Maghrib Time
  112. maghribTime = zuhrTime + (Durinal_Arc / 15);
  113.  
  114.  
  115. double Esha_Arc = radToDeg(acos((sin(degToRad(ishaTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  116. // 5) Isha Time
  117. ishaTime = zuhrTime + (Esha_Arc / 15);
  118.  
  119. // 0) Fajr Time
  120. double Fajr_Arc = radToDeg(acos((sin(degToRad(fajrTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  121. fajrTime = zuhrTime - (Fajr_Arc / 15);
  122.  
  123. return;
  124. }
  125.  
  126. /*
  127.  Prayers calculator end
  128. */
  129.  
  130.  
  131. /*
  132.  Personal code. Calculating for Ankara.
  133.  
  134. Date: 12-11-2024
  135. Longitude: 32.8597
  136. Latitude: 39.9334
  137. Time Zone: +3
  138. Fajr Twilight: -19.5
  139. Esha Twilight: -17.5
  140. */
  141.  
  142. int main() {
  143. double fajr, sunRise, zuhr, asr, maghrib, isha;
  144. calcPrayerTimes(2024,11,12, 32.8597, 39.9334, 3, -18, -17,
  145. fajr, sunRise, zuhr, asr, maghrib, isha);
  146.  
  147. int hours, minutes;
  148.  
  149. doubleToHrMin(fajr, hours, minutes);
  150. std::cout << "Fajr: " << hours << ":" << minutes << std::endl;
  151.  
  152. doubleToHrMin(sunRise, hours, minutes);
  153. std::cout << "Sunrise: " << hours << ":" << minutes << std::endl;
  154.  
  155. doubleToHrMin(zuhr, hours, minutes);
  156. std::cout << "Zuhr: " << hours << ":" << minutes << std::endl;
  157.  
  158. doubleToHrMin(asr, hours, minutes);
  159. std::cout << "Asr: " << hours << ":" << minutes << std::endl;
  160.  
  161. doubleToHrMin(maghrib, hours, minutes);
  162. std::cout << "Maghrib: " << hours << ":" << minutes << std::endl;
  163.  
  164. doubleToHrMin(isha, hours, minutes);
  165. std::cout << "Isha: " << hours << ":" << minutes << std::endl;
  166. }
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Fajr: 5:57
Sunrise: 7:30
Zuhr: 12:32
Asr: 15:13
Maghrib: 17:35
Isha: 19:3