Skip to content

Instantly share code, notes, and snippets.

@AmanSinghBhogal
Last active January 11, 2021 04:10
Show Gist options
  • Save AmanSinghBhogal/c11818e4df2bf48c33df646e7cc6dcd5 to your computer and use it in GitHub Desktop.
Save AmanSinghBhogal/c11818e4df2bf48c33df646e7cc6dcd5 to your computer and use it in GitHub Desktop.
Age in years and Day
//Program to find the age in days.
#include<stdio.h>
#include<process.h>
int main()
{
system("cls");
int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int d1,d2,m1,m2,y1,y2,c1,c2=0,c3=0,c4=0,c5=0,k=0;
printf("Enter Current Date in dd-mm-yy fromat:");
scanf("%d/%d/%d",&d1,&m1,&y1);
printf("Enter Birth Date in dd-mm-yy fromat:");
scanf("%d/%d/%d",&d2,&m2,&y2);
if( d1<1 || d2<1 || d1>31 || d2>31 || m1<1 || m2<1 || m1>12 || m2>12 || y2>y1 )
{
printf("Enter a valid date next time.!");
exit (0);
}
//calculating c1.
if(y2%4!=0 || m2!=2)
c1=m[(m2-1)] - d2;
else
{
c1= 29 - d2;
}
// now calculating c2.
for(int i=m2;i<12;i++)
{
c2 += m[i];
}
// now calculating c3.
for(int i=(y2+1);i<y1;i++)
{
if(i%4!=0)
c3 +=365;
else
{
c3 += 366;
k+=1;
}
}
//now calculating c4.
for(int i=0;i<(m1-1);i++)
{
c4 += m[i];
}
//now calculating c5.
c5 = d1;
//now finding total number of days
int age= c1+c2+c3+c4+c5;
printf("Your Age in days is:%d\n",age);
//now converting it to number of years and number of months.
int yr=(age-k)/365;
int months=((age-k)%365)/31;
int days=(age-(yr*365 +k)-(months*31));
printf("You have been on earth for like %d Days %d Months %d Years.\n",days,months,yr);
printf("Thank you");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment