Age Calculator Mini Project using C program with Source Code

Introduction

The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems. Low-level memory access, a small collection of keywords, and a clean style are all qualities that make C language excellent for system programming, such as operating system or compiler development.

Why Learn C Programming Language?

Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.

Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.

Age Calculator

The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build age calculator mini project using c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.

Source Code

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

// for sleep() function
#include <time.h>
// checking given year is leap year or not
// if yes flag is returned
// if no nothing is changed
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;
// input date of birth
printf("Enter your date of birth (DD/MM/YYYY): ");
scanf("%d/%d/%d",&days,&month, &year);
// getting the 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 your 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;
}

// printing the calculated age
printf("\n ==> Hey you are  %d years %d months and %d days old !!!\n", year, month, days);
return 0;
}

Output

You may Also Like

SHARE Age Calculator Mini Project using C program with Source Code

You may also like...

Leave a Reply

Your email address will not be published.

Share