C Code for Age

This c programming code is used to find the age . You can select the whole c code by clicking the select option and can use it. When you click text, the code will be changed to text format. This c program code will be opened in a new pop up window once you click pop-up from the right corner. You can just copy, paste this c code and use it to find the age .

#include<stdio.h>
#include<time.h>

int leapYearFeb(int year, int mon) //Leap year checking
{
    int flag = 0;
    if (year % 100 == 0) 
    {
        if (year % 400 == 0) 
        {
            if (mon == 2) 
            {
                flag = 1;
            }
        }
    } 
    else if (year % 4 == 0) 
    {
        if (mon == 2) 
        {
            flag = 1;
        }
    }
    return (flag);
}

int main(void)
{
    int daysInMon[] = {31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31};  //Days in month
    int days, month, year;
    char dob[100];
    time_t ts;
    struct tm *ct;

    days=12;
    month=8;
    year=1992;
    printf("\nDate of Birth: %d/%d/%d", days, month, year);
    
    ts = time(NULL);   //Find the current month
    ct = localtime(&ts);
    printf("\nCurrent Date: %d/%d/%d",ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
    days = daysInMon[month - 1] - days + 1;

    if (leapYearFeb(year, month)) //Checking the given year Leap or not
    {
        days = days + 1;
    }
    
    /*Calculating the num of year, month and date*/
    days = days + ct->tm_mday;
    month = (12 - month) + (ct->tm_mon);
    year = (ct->tm_year + 1900) - year - 1;

    if (leapYearFeb((ct->tm_year + 1900), (ct->tm_mon + 1))) 
    {
        if (days >= (daysInMon[ct->tm_mon] + 1)) 
        {
            days = days - (daysInMon[ct->tm_mon] + 1);
            month = month + 1;
        }
    } 
    else if (days >= daysInMon[ct->tm_mon]) 
    {
        days = days - (daysInMon[ct->tm_mon]);
        month = month + 1;
    }

    if (month >= 12) 
    {
        year = year + 1;
        month = month - 12;
    }

    /*Result portion*/
    
    printf("\nAge: %d year %d months and %d days", year, month, days);
    return 0;
  }

Other Programming Codes
Online Calculator : Age Calculator
Click on the select code link to copy and paste this free c program code for age calculator.

english Calculators and Converters


Sitemap