0% found this document useful (0 votes)
21 views

#Include #Include #Include Using Namespace Int: Declaring Functions

The document contains code to display the current date and time in C++ using the time.h library. It uses the time() and localtime() functions to get a time_t variable representing the current time. It then displays the individual components of the struct tm returned by localtime like seconds, minutes, hours, day, month, year, etc. It also displays the current date and time as strings.

Uploaded by

Romeo Balingao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

#Include #Include #Include Using Namespace Int: Declaring Functions

The document contains code to display the current date and time in C++ using the time.h library. It uses the time() and localtime() functions to get a time_t variable representing the current time. It then displays the individual components of the struct tm returned by localtime like seconds, minutes, hours, day, month, year, etc. It also displays the current date and time as strings.

Uploaded by

Romeo Balingao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>sfds

#include<cmath>
#include <ctime>
using namespace std;

int main()
{

time_t t = time(NULL);
tm* tPtr = localtime(&t);
cout << "\n\n Display the Current Date and Time :\n";
cout << "----------------------------------------\n";
cout << " seconds = " << (tPtr->tm_sec) << endl;
cout << " minutes = " << (tPtr->tm_min) << endl;
cout << " hours = " << (tPtr->tm_hour) << endl;
cout << " day of month = " << (tPtr->tm_mday) << endl;
cout << " month of year = " << (tPtr->tm_mon)+1 << endl;
cout << " year = " << (tPtr->tm_year)+1900 << endl;
cout << " weekday = " << (tPtr->tm_wday )<< endl;
cout << " day of year = " << (tPtr->tm_yday )<< endl;
cout << " daylight savings = " <<(tPtr->tm_isdst )<< endl;
cout << endl;
cout << endl;

cout << " Current Date: " <<(tPtr->tm_mday)<<"/"<< (tPtr->tm_mon)+1 <<"/"<<


(tPtr->tm_year)+1900<< endl;
cout << " Current Time: " << (tPtr->tm_hour)<<":"<< (tPtr->tm_min)<<":"<< (tPtr-
>tm_sec) << endl;
cout << endl;
system("pause");
return 0;
}

Declaring Functions

// declaring functions prototypes


#include <iostream>
using namespace std;

void odd (int a);


void even(int a);

int main()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);

system("pause");
return 0;
}

void odd(int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even(int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}

Arrays

// arrays example
#include <iostream>
using namespace std;
int main ()
{
int Integer[3];

cout<< "Enter Integer 1: "; cin>>Integer[0];


cout<< "Enter Integer 2: "; cin>>Integer[1];

Integer[2] = ((Integer[0]*5) + Integer[1])/2;

cout<< "The Total Value of Integer 3 is: " << Integer[2] << "\n";
system("pause");
return 0;
}

You might also like