w3resource

C++ Date: Exercises, Practice, Solution

C++ Date [6 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a C++ program to get the current date and time.
The current date and time is:
Tue Mar 15 14:28:05 2022
N.B.: The result may vary for your current system date and time.
Click me to see the sample solution

2. Write a C++ program to get the day of the week from a given date. Return the day as a string. The format of the day is MM/DD/YYYY.
Date: 02/05/1980 Day of the week: Tuesday
Date: 01/24/1990 Day of the week: Wednesday
Date: 01/05/2019 Day of the week: Saturday
Date: 11/17/2022 Day of the week: Thursday
Click me to see the sample solution

3. Write a C++ program to convert a given year to a century.
Year: 1900, Century of the year: 19
Year: 1999, Century of the year: 20
Year: 2000, Century of the year: 20
Year: 2010, Century of the year: 21
Year: 2020, Century of the year: 21
Click me to see the sample solution

4. Write a C++ program to check whether a given year is a leap year or not.
Is Leap year: 1900? Century of the year: 0
Is Leap year: 1999? Century of the year: 0
Is Leap year: 2000? Century of the year: 1
Is Leap year: 2010? Century of the year: 0
Is Leap year: 2020? Century of the year: 1
Click me to see the sample solution

5. Write a C++ program to compute the number of days in a month for a given year and month.
Input Year: 2019
Input Month: 04
Number of days of the year 2019 and month 4 is: 30
Click me to see the sample solution

6. Write a C++ program to count the number of days between two given dates.
Date Format -> YYYY-MM-DD
Days between 2022/01/31 to 2022/01/01 = 30
Days between 2000/01/31 to 2019/02/01 = 6938
Days between 1980/05/31 to 1995/12/12 = 5677
Click me to see the sample solution

CPP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

Why does the C++ map type argument require an empty constructor when using []?

This issue comes with operator[]. Quote from SGI documentation:

data_type&operator[](constkey_type& k) - Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type().

If you don't have default constructor you can use insert/find functions. Following example works fine:

myMap.insert(std::map<int, MyClass>::value_type ( 1, MyClass(1) ) );
myMap.find( 1 )->second;

Ref: https://bit.ly/3PQBRJi

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook