C++ Program to Convert Years of Age into Months, Days, Hours And Minutes
This C++ program asks the user to enter the years of age as input. And this program will convert years of age into 4 formats i.e. months, days, hours and minutes.
Formulas To Convert Years Of Age Into Months, Days, Hours And Minutes:
There are some formulas which are used to convert time of one format into another:
1: Months = years * 12
To convert years of age into months, we just need one information i.e. number of years. So we will make a function which takes just no. of years as input and converts it into another time standard. We also know that one year contains twelve months in it. So, we will multiply no. of years with 12 to get actual number of months.
2: Days = years * 365
A common year has 365 days. Leap year has an additional day i.e. 366 days. For the sake of simplicity of the program, we will take one year as 365 days. If you want to get more accurate result then you can use Days = Years * 365.
3: Hours = days * 24
It is obvious that one day has 24 hours. We have calculated no. of days in above formula. So multiplying this number with 24 can give us accurate number of hours in the age.
4: Minutes = Hours * 60
Collectively 60 minutes make one complete hour. To get no. of minutes, we will have to multiply the no. of hours with 60.
Recommended reading: How to Check Whether A Number is Prime Or Not in C++
C++ Source Code to Convert Years of Age into Months:
using namespace std;
cout <<"ENTER AGE IN YEARS = ";
cout<<"\nAGE IN MONTHS = "<<months<<endl;
cout<<"AGE IN DAYS = "<<days<<endl;
cout<<"AGE IN HOURS = "<<hours<<endl;
cout<<"AGE IN MINUTES = "<<minutes;
Output of Program:
First of all we declared five variables to store values in different time standards. All the variable are of int data type. After that we used formulas to do calculations and stored values in the corresponding variables. At the end, we printed the results on screen.
Recommended reading: How to Find Grade of a Student in C++
Let’s dry run this code by giving it a hypothetical value 14.
Months = Years * 12 =14 * 12 = 168
Days = Years * 365 = 14 * 365 = 5110
Hours = Days * 24 = 5110 * 24 = 122640
Minutes = Hours * 60 = 7358400
If you have any question then you can ask me in comments. I would like to answer them.
You can also Click Here To Download Exercise Files.