programa para calcular la edad

Dada la fecha actual y la fecha de nacimiento, encuentre la edad actual. 
Ejemplos: 
 

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

Al calcular la diferencia en dos fechas, solo debemos realizar un seguimiento de dos condiciones que lo harán. 
 

  • Si la fecha actual es menor que la fecha de nacimiento, entonces ese mes no se cuenta, y para restar fechas sumamos el número de días del mes a la fecha actual para obtener la diferencia en las fechas.
  • Si el mes actual es menor que el mes de nacimiento, entonces el año actual no se tiene en cuenta ya que este año no se ha completado y para obtener la diferencia de meses, restamos sumando 12 al mes actual.
  • Al final, solo necesitamos restar los días, meses y años para obtener la diferencia después de tratar las dos condiciones.

A continuación se muestra la implementación del enfoque anterior: 
 

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 than 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;
}

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 than 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);
    }
}

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 than 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 = 7
current_month = 12
current_year = 2017
         
# birth dd//mm//yyyy
birth_date = 16
birth_month = 12
birth_year = 2009
 
findAge(current_date, current_month, current_year,
        birth_date, birth_month, birth_year)

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 than
        // 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.

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
    // than 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.
?>

Javascript

<script>
 
// Javascript program for age calculator
 
// function to calculate current age
function findAge(current_date, current_month, current_year, birth_date,
             birth_month, birth_year)
{
    // days of every month
    month = [31, 28, 31, 30, 31, 30, 31,
                        31, 30, 31, 30, 31 ]
 
    // if birth date is greater than 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
    var calculated_date = current_date - birth_date;
    var calculated_month = current_month - birth_month;
    var calculated_year = current_year - birth_year;
 
    // print the present age
    document.write("Present Age<br>Years: "+(calculated_year)+" ");
    document.write("Months: "+calculated_month+" ");
    document.write("Days: "+calculated_date+" ");
}
 
// driver code to check the above function
// current dd// mm/yyyy
var current_date = 7;
var current_month = 12;
var current_year = 2017;
// birth dd// mm// yyyy
var birth_date = 16;
var birth_month = 12;
var birth_year = 2009;
// function call to print age
findAge(current_date, current_month, current_year,
            birth_date, birth_month, birth_year);
 
</script>
Producción

Present Age
Years: 7  Months: 11  Days: 22

Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1) 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *