Given current date and birth date, find the present age.
Examples:
Input : Birth date = 07/09/1996
Present date = 07/12/2017
Output : Present Age = Years: 21 Months: 3 Days: 0
t Age = Years: 7 Months: 11 Days: 21
While calculating the difference in two dates we need to just keep track of two conditions that will do.
- If the current date is less than that of the birth date, then that month is not counted, and for subtracting dates we add number of month days to the current date so as to get the difference in the dates.
- If the current month is less than the birth month, then the current year is not taken into count as this year has not been completed and for getting the difference of months, we subtract by adding 12 to the current month.
- At the end we just need to subtract the days, months and years to get the difference after the two conditions are dealt with.
Below is the implementation of the above approach :
C++
// c program for age calculator #include <stdio.h> #include <stdlib.h> // function to calculate current age void findAge(int current_date, int current_month, int current_year, int birth_date, int birth_month, int birth_year) { // days of every month int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // if birth date is greater then current birth // month then do not count this month and add 30 // to the date so as to subtract the date and // get the remaining days if (birth_date > current_date) { current_date = current_date + month[birth_month - 1]; current_month = current_month - 1; } // if birth month exceeds current month, then do // not count this year and add 12 to the month so // that we can subtract and find out the difference if (birth_month > current_month) { current_year = current_year - 1; current_month = current_month + 12; } // calculate date, month, year int calculated_date = current_date - birth_date; int calculated_month = current_month - birth_month; int calculated_year = current_year - birth_year; // print the present age printf("Present Age\nYears: %d Months: %d Days:" " %d\n", calculated_year, calculated_month, calculated_date); } // driver code to check the above function int main() { // current dd// mm/yyyy int current_date = 7; int current_month = 12; int current_year = 2017; // birth dd// mm// yyyy int birth_date = 16; int birth_month = 12; int birth_year = 2009; // function call to print age findAge(current_date, current_month, current_year, birth_date, birth_month, birth_year); return 0; } |
chevron_right
filter_none
Java
// Java program for age calculator import java.io.*; class GFG { static void findAge(int current_date, int current_month, int current_year, int birth_date, int birth_month, int birth_year) { int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // if birth date is greater then current // birth_month, then donot count this month // and add 30 to the date so as to subtract // the date and get the remaining days if (birth_date > current_date) { current_month = current_month - 1; current_date = current_date + month[birth_month - 1]; } // if birth month exceeds current month, // then do not count this year and add // 12 to the month so that we can subtract // and find out the difference if (birth_month > current_month) { current_year = current_year - 1; current_month = current_month + 12; } // calculate date, month, year int calculated_date = current_date - birth_date; int calculated_month = current_month - birth_month; int calculated_year = current_year - birth_year; // print the present age System.out.println("Present Age"); System.out.println("Years: " + calculated_year + " Months: " + calculated_month + " Days: " + calculated_date); } public static void main(String[] args) { // present date int current_date = 7; int current_month = 12; int current_year = 2017; // birth dd// mm// yyyy int birth_date = 16; int birth_month = 12; int birth_year = 2009; // function call to print age findAge(current_date, current_month, current_year, birth_date, birth_month, birth_year); } } |
chevron_right
filter_none
Python
# Python program to calculate range def findAge(current_date, current_month, current_year, birth_date, birth_month, birth_year): # if birth date is greater then current birth_month # then donot count this month and add 30 to the date so # as to subtract the date and get the remaining days month =[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if (birth_date > current_date): current_month = current_month - 1 current_date = current_date + month[birth_month-1] # if birth month exceeds current month, then # donot count this year and add 12 to the # month so that we can subtract and find out # the difference if (birth_month > current_month): current_year = current_year - 1; current_month = current_month + 12; # calculate date, month, year calculated_date = current_date - birth_date; calculated_month = current_month - birth_month; calculated_year = current_year - birth_year; # print present age print"Present Age" print("Years:", calculated_year, "Months:", calculated_month, "Days:", calculated_date) # driver code current_date = 7current_month = 12current_year = 2017 # birth dd//mm//yyyy birth_date = 16birth_month = 12birth_year = 2009 findAge(current_date, current_month, current_year, birth_date, birth_month, birth_year) |
chevron_right
filter_none
C#
// C# program for age calculator using System; class GFG { static void findAge(int current_date, int current_month, int current_year, int birth_date, int birth_month, int birth_year) { int []month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // if birth date is greater then // current birth_month, then donot // count this month and add 30 to the // date so as to subtract the date and // get the remaining days if (birth_date > current_date) { current_month = current_month - 1; current_date = current_date + month[birth_month - 1]; } // if birth month exceeds current month, // then do not count this year and add // 12 to the month so that we can // subtract and find out the difference if (birth_month > current_month) { current_year = current_year - 1; current_month = current_month + 12; } // calculate date, month, year int calculated_date = current_date - birth_date; int calculated_month = current_month - birth_month; int calculated_year = current_year - birth_year; // print the present age Console.WriteLine("Present Age"); Console.WriteLine("Years: " + calculated_year + " Months: " + calculated_month + " Days: " + calculated_date); } // driver code to check the above function public static void Main() { // present date int current_date = 7; int current_month = 12; int current_year = 2017; // birth dd// mm// yyyy int birth_date = 16; int birth_month = 12; int birth_year = 2009; // function call to print age findAge(current_date, current_month, current_year, birth_date, birth_month, birth_year); } } //This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program for // age calculator // function to calculate // current age function findAge(int $current_date, int $current_month, int $current_year, int $birth_date, int $birth_month, int $birth_year) { // days of every month $month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); // if birth date is greater // then current birth month // then do not count this // month and add 30 to the // date so as to subtract // the date and get the // remaining days if ($birth_date > $current_date) { $current_date = $current_date + $month[$birth_month - 1]; $current_month = $current_month - 1; } // if birth month exceeds // current month, then do // not count this year and // add 12 to the month so // that we can subtract and // find out the difference if ($birth_month > $current_month) { $current_year = $current_year - 1; $current_month = $current_month + 12; } // calculate date, month, year $calculated_date = $current_date - $birth_date; $calculated_month = $current_month - $birth_month; $calculated_year = $current_year - $birth_year; // print the present age echo "Present Age\nYears:",$calculated_year, " " , "Months:", $calculated_month, " ", "Days:", $calculated_date; } // Driver Code // current dd// mm/yyyy $current_date = 7; $current_month = 12; $current_year = 2017; // birth dd// mm// yyyy $birth_date = 16; $birth_month = 12; $birth_year = 2009; // function call to print age findAge($current_date, $current_month, $current_year, $birth_date, $birth_month, $birth_year); // This code is contributed by anuj_67. ?> |
chevron_right
filter_none
Output:
Present Age Years: 7 Months: 11 Days: 22
Recommended Posts:
- Python program to calculate age in year
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Program to Calculate Body Mass Index (BMI)
- Program to calculate distance between two points
- Program to calculate volume of Ellipsoid
- Program to calculate area and perimeter of Trapezium
- Program to calculate area and volume of a Tetrahedron
- Program to calculate Volume and Surface area of Hemisphere
- Program to calculate value of nCr
- Program to calculate area of a Tetrahedron
- Program to calculate area and perimeter of equilateral triangle
- Program to calculate Area Of Octagon
- Program to calculate GST from original and net prices
- Program to calculate volume of Octahedron
- Program to calculate area of Enneagon
- Program to calculate area of Circumcircle of an Equilateral Triangle
- Program to calculate Percentile of a student based on rank
- Program to calculate area of an Circle inscribed in a Square
- Program to calculate distance between two points in 3 D
- Program to calculate Profit Or Loss
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m

