Ad Code

Program To Get The Day Of Birth Date Using C++.

Welcome to Codingfizz,

In this article, Write a program to get the day of birth date using C++. This program will work for both leap and non-leap years. 

You should have knowledge of the if-else, STL Maps, functions, and knowledge about how to check if a year is a leap year or not.

Note: This program will work above the year 1900.

Leap Year: All years are perfectly divisible by 4 and 400 for century years.

For example, 

2020, 2000, 2004, 1968, 2012

The following formula is used for getting the day of the birth date.

Birth Day  = (Date + Month + Year + Leap Year) / 7

Here, The year is equal to subtracting the actual year from 1900

For example, 

year = abs(1900 - 2000);

year = 100;

Then, check for the leap year and put all the values in the formula.

Program: To Get The Day Of Birth Date Using C++.


#include <iostream>
#include <map>
using namespace std;

int leapYear(int year) {

  int rem1, rem2;
  
  rem1 = year % 400;
  rem2 = year % 4;

  if (rem1 == 0) {
    return year / 4;
  } 
  else if (rem2 == 0) {
     return year / 4;
  } 
  else {
    return year / 4;
  }
}

int main() {

  // variables initialization
  int d, m, y, ly, day;
  map<int, int> month;

  // store the value of months
  month = {{1, 0}, {2, 3}, {3, 3}, {4, 6},  {5, 1},  {6, 4},
           {7, 6}, {8, 2}, {9, 5}, {10, 0}, {11, 3}, {12, 5}};

  // inputs from user
  cout << "Enter Day: ";
  cin >> d;
  cout << "Enter Month: ";
  cin >> m;
  cout << "Enter Year: ";
  cin >> y;

  // subtract from 1900
  y = abs(1900 - y);

  ly = leapYear(y);

  day = (d + month[m] + y + ly) % 7;

  // getting day
  if (day == 0) {
    cout << "Sunday";
  } else if (day == 1) {
    cout << "Monday";
  } else if (day == 2) {
    cout << "Tuesday";
  } else if (day == 3) {
    cout << "Wednesday";
  } else if (day == 4) {
    cout << "Thursday";
  } else if (day == 5) {
    cout << "Friday";
  } else if (day == 6) {
    cout << "Saturday";
  }

  return 0;
}
Output:

For the non-leap year,

Enter Day: 30
Enter Month: 4
Enter Year: 2003
Wednesday

For the leap year,

Enter Day: 30
Enter Month: 4
Enter Year: 2000
Sunday

So we have finally discussed our topic which is Program To Get The Day Of Birth Date Using C++.

You get a clear understanding of everything in this article. If you want to ask anything. Please feel free to contact us by commenting down below. Thank you

Post a Comment

0 Comments

Ad Code