English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C ++The program calculates the number of odd days in a given year

Given a positive integer value n, the task is to generate the odd days until the given year n.

Example

Input-: days = 500
Output-: The number of odd days are: 5
Input-: days = 400
Output-: The number of odd days are: 0

How to calculate the number of odd days in a given year

When calculating the number of odd days, the first thing we need to check is whether the given year is a leap year, because if it is a leap year, the number of odd days will change. If the year can be100 or400 divisible by but not4divisible by but not7to calculate the modulus of the total number of days, which is the number of days in a week. Therefore, common years include1Odd days, leap years include2odd days.

The method we use in the given program is as follows-

  • Enter the number of days as input

  • Check if the year is a leap year or a common year

  • Calculate the modulus of the total number of days by dividing the total number of days by the modulus, which is the number of days in a week. Therefore, common years include

  • Display the final result as the number of days from1Number of days to n

Algorithm

Start
Step 1-> Declare function to calculate the number of odd days in a given year
   int cal_odd(int days)
   declare int cal_1 = days / 100
   declare int cal_2 = days / 400
   declare int check_leap = days >> 2
   declare int temp = days - check_leap
   IF (cal_1)
      set temp += cal_1
      Set check_leap -= cal_1
   End
   IF (cal_2)
      Set temp -= cal_2
      Set check_leap += cal_2
   End
   declare int final_days = temp + check_leap * 2
   Declare int odd = final_days % 7
   return odd
step 2->In main() Declare int days = 500
   call cal_odd(days)
Stop

Example

#include <iostream>
using namespace std;
//Calculate the number of odd days in a given year
int cal_odd(int days) {
    int cal_1 = days / 100;
    int cal_2 = days / 400;
    int check_leap = days >> 2;
    int temp = days - check_leap;
    if (cal_1) {
        temp += cal_1;
        check_leap -= cal_1;
    return 0;
    if (cal_2) {
        temp -= cal_2;
        check_leap += cal_2;
    return 0;
    int final_days = temp + check_leap * 2;
    int odd = final_days % 7;
    return odd;
return 0;
int main() {
    int days = 500;
    cout << "Odd days are: " << cal_odd(days);
    return 0;
return 0;

}

Output result 5