15,745,574 members
See more:
To get our age in days using C program.

What I have tried:

C
#include<stdio.h>
int main(){
int date,month,year,cur_year,cur_date,cur_month,i,n,s=0,k,p,m,a[7]={1,3,5,7,8,10,12},b[4]={4,6,9,11};
printf("Enter date of birth: ");
scanf("%d",&date);
printf("Enter month of birth in number: ");
scanf("%d",&month);
printf("Enter year of birth: ");
scanf("%d",&year);
printf("Enter current date: ");
scanf("%d",&cur_date);
printf("Enter current month: ");
scanf("%d",&cur_month);
printf("Enter current year: ");
scanf("%d",&cur_year);

//for getting number of days remained in your birth month

for(i=0;i<=10;i++){
if(month==a[i])
p = 31 - date;
if(month==b[i])
p = 30 - date;
}
if(month==2){
if(year%4==0)
p = 29 - date;
else
p = 28 - date;
}

//for getting number of days in remaining months in the birth year

switch(month){
case 1:{
s = (31*6)+(30*4);
if(year%4==0)
k = s + 29;
else
k = s + 28;
break;
}
case 2:{
k = (31*6)+(30*4);
break;
}
case 3:{
k = (31*5)+(30*4);
break;
}
case 4:{
k = (31*5)+(30*4);
break;
}
case 5:{
k = (31*4)+(30*3);
break;
}
case 6:{
k = (31*4)+(30*2);
break;
}
case 7:{
k = (31*3)+(30*2);
break;
}
case 8:{
k = (31*2)+(30*2);
break;
}
case 9:{
k = (31*2)+30;
break;
}
case 10:{
k = 30 + 31;
break;
}
case 11:{
k = 31;
break;
}
}

//for getting number of days in years after your birth year and before present year

for(n=year+1;n<=cur_year-1;n++){
if(n%4==0)
s=s+366;
else
s=s+365;
}

//for getting number of days passed in the current year till todays month

switch(cur_month){
case 12:{
if(cur_year%4==0)
m = (31*6)+(30*4)+29;
else
m = (31*6)+(30*4)+28;
break;
}
case 11:{
if(cur_year%4==0)
m = (31*6)+(30*3)+29;
else
m = (31*6)+(30*3)+28;
break;
}
case 10:{
if(cur_year%4==0)
m = (31*5)+(30*3)+29;
else
m = (31*5)+(30*3)+28;
break;
}
case 9:{
if(cur_year%4==0)
m = (31*5)+(30*2)+29;
else
m = (31*5)+(30*2)+28;
break;
}
case 8:{
if(cur_year%4==0)
m = (31*4)+(30*2)+29;
else
m = (31*4)+(30*2)+28;
break;
}
case 7:{
if(cur_year%4==0)
m = (31*3)+(30*2)+29;
else
m = (31*3)+(30*2)+28;
break;
}
case 6:{
if(cur_year%4==0)
m = (31*3)+(30*1)+29;
else
m = (31*3)+(30*1)+28;
break;
}
case 5:{
if(cur_year%4==0)
m = (31*2)+(30*1)+29;
else
m = (31*2)+(30*1)+28;
break;
}
case 4:{
if(cur_year%4==0)
m = (31*2)+29;
else
m = (31*2)+28;
break;
}
case 3:{
if(cur_year%4==0)
m = (31*1)+29;
else
m = (31*1)+28;
break;
}
case 2:{
m = 31;
break;
}
}

//number of months passed in the currents month is included below in the sum

printf("Current age in days is %d",p+k+s+m+cur_date);
return 0;
}
Posted
Updated 27-Jun-23 16:13pm
v2

## Solution 1

That's ... not nice code ... to the point where I'm not going to wade through it (and "wade" is exactly what we'd have to do).

Write yourself a function which accepts a date, and returns the day number: Jan 1st 1990 is 1, Jan 2nd 1990 is 2, ... Dec 31st 1990 is 366, Jan 1st 1991 is 1, ... Dec 31st 1991 is 365, ...
Now it's trivial: get the day number for the birth year, get the day number for Dec31 of that year. Subtract.
Get the number of days for each year between the following year and last year - add them up and include the difference you calculated earlier. You can use the same function.
Get the day number for today. Add it in.

Because you write one function and can test it carefully, you can reuse it - and the rest of your code becomes really obvious and easy to read ...

BTW: You don't need to get the current date from the user: C libraries include code to do that for you: https://www.techiedelight.com/print-current-date-and-time-in-c/[^]
And it might be a good idea to use the same struct to hold your birthdate ... hint, hint.

CPallini 28-Jun-23 1:51am
5.

## Solution 2

Quote:
What is wrong with this code?

a and b are used in same loop, since they are different size, it is a bad idea.
Looping beyond the end of both array is a bad idea too.
C++
int a[7]={1,3,5,7,8,10,12},b[4]={4,6,9,11};
...

for(i=0;i<=10;i++){
if(month==a[i])
p = 31 - date;
if(month==b[i])
p = 30 - date;
}

In memory, you can get something like:
1 // a[0]
3 // a[1]
5 // a[2]
7 // a[3]
8 // a[4]
10 // a[5]
12 // a[6]
4 // b[0] and a[7]
6 // b[1] and a[8]
9 // b[2] and a[9]
11 // b[3] and a[10]
unknown // b[4]
unknown // b[5]

because C do not check for ends of arrays.

CPallini 28-Jun-23 1:51am
5.
Patrice T 28-Jun-23 9:38am
Thank you