# 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.]

1. Write a C++ program to get the current date and time. Go to the editor
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 given date is MM/DD/YYYY. Go to the editor
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 century. Go to the editor
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 not. Go to the editor
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 of a month from a given year and month. Go to the editor
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. Go to the editor
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.

﻿

## C++ Programming: Tips of the Day

How do I execute a command and get the output of the command within C++ using POSIX?

```#include<cstdio>
#include<iostream>
#include<memory>
#include<stdexcept>
#include<string>
#include<array>

std::string exec(constchar* cmd){
std::array>char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)>pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
Pre-C++11 version:
#include<iostream>
#include<stdexcept>
#include<stdio.h>
#include<string>

std::string exec(constchar* cmd){
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
} ```

Replace popen and pclose with _popen and _pclose for Windows.

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