BY TEJAS
Age Calculator is a very lovable, program to begineers and also to the users. It is pretty, functional and fascinating to see that output of our age with minutes, and seconds.
So let's take a turn to our program.
While creating an "age calculator" first, we see what logic is behind the program "Program is compared the user entered date with current date and show how old you are." It's seen a little bit hard, but not that much hard this program.
In this, we use If.....Else
statements are many times not any arrays
, Pointers
are used in this program we use If....Else
statement and Nested If.....Else
statements.
It is a very simple example.
In below video shows program
below here is a C code program and output of c program.
Extension of C Program is .c
#include <stdio.h>
#include <time.h>
/*check given year is leap year or not*/
int isLeapYear(int year, int mon)
{
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()
{
int DaysInMon[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int days, month, year;
char dob[100];
time_t ts;
struct tm *ct;
/* enter date of birth */
printf("Enter your date of birth (DD/MM/YYYY): ");
scanf("%d/%d/%d",&days,&month, &year);
/*get current date.*/
ts = time(NULL);
ct = localtime(&ts);
printf("Current Date: %d/%d/%d\n",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
days = DaysInMon[month - 1] - days + 1;
/* leap year checking*/
if (isLeapYear(year, month))
{
days = days + 1;
}
/* calculating age in no of days, years and months */
days = days + ct->tm_mday;
month = (12 - month) + (ct->tm_mon);
year = (ct->tm_year + 1900) - year - 1;
/* checking for leap year feb - 29 days */
if (isLeapYear((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;
}
/* print age */
printf("\n## Hey you are %d years %d months and %d days old. ##\n", year, month, days);
return 0;
}
Enter your date of birth (DD/MM/YYYY): 07/03/2002
Current Date: 23/2/2020
## Hey you are 17 years 11 months and 19 days old. ##
#include <stdio.h>
#include <time.h>
/*check given year is leap year or not*/
int isLeapYear(int year, int mon)
{
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()
{
int DaysInMon[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int days, month, year;
char dob[100];
time_t ts;
struct tm *ct;
/* enter date of birth */
printf("Enter your date of birth (DD/MM/YYYY): ");
scanf("%d/%d/%d",&days,&month, &year);
/*get current date.*/
ts = time(NULL);
ct = localtime(&ts);
printf("Current Date: %d/%d/%d\n",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
days = DaysInMon[month - 1] - days + 1;
/* leap year checking*/
if (isLeapYear(year, month))
{
days = days + 1;
}
/* calculating age in no of days, years and months */
days = days + ct->tm_mday;
month = (12 - month) + (ct->tm_mon);
year = (ct->tm_year + 1900) - year - 1;
/* checking for leap year feb - 29 days */
if (isLeapYear((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;
}
/* print age */
printf("\n## Hey you are %d years %d months and %d days old. ##\n", year, month, days);
return 0;
}
Enter your date of birth (DD/MM/YYYY): 07/03/2002
Current Date: 23/2/2020
## Hey you are 17 years 11 months and 19 days old. ##