Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Count Dracula birthday is on 29/02/1600.. Can you calculate how many birthdays he managed to celebrate till the year 2022. write a code in c

What I have tried:

geeks for geeks iyiiiiiiiiiiiiiiiiiiii
Posted
Updated 8-Aug-23 0:07am
v2

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

I'd suggest you start here: Century leap year - Wikipedia[^] as not every fourth year has a leap year!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
This is a simple mathematical problem, (presumably) taking into account the fact that he was born on a date that occurs only once every four years. So think about the calculations you need to make to get to the answer. Once you have done that it is a simple matter to write the C code to perform the actual calculations.
 
Share this answer
 
Quote:
Count dracula birthday is on 29/02/1600.. Can you calculate how many birthdays he managed to celebrate till the year 2022. Write a code in C

Not more difficult than calculating how many of your birthdays you have celebrated til 2022.
You should be able to solve this by yourself.
For more help, tell us what is your problem.
 
Share this answer
 
Easy peasy if you would have used Google, could have been done quicker than to wait for answers here - calculate birthdays using C[^]
The first one that popped up is coming with a tutorial and sample code - C program to calculate age[^]

[EDIT]
To cover for the leap years, the search were just as easy as the above, with sample code - Age Calculator program in C[^]

Using the last example code, the output were -
Enter your date of birth (DD/MM/YYYY): 29/02/1600
Current Date: 8/8/2023

## Hey you are  423 years 5 months and 9 days old. ##


The code used -
C
/*Age Calculator (C program to calculate age).*/
 
#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;
}
 
Share this answer
 
v3
Comments
OriginalGriff 8-Aug-23 6:23am    
Except ... check the given birthdate for Dracula. :D
Andre Oosthuizen 8-Aug-23 8:00am    
LOL, the answer will be completely wrong though as per Google, Dracula were born on the 8th of November 1431 - Vlad III "Tepes" Basarab (Dracula/Alucard) Birthday[^] which makes him 591 years 9 months and 0 days old :)

I updated my solution as well to cover for leap years.
Richard Deeming 8-Aug-23 8:35am    
Worse than that - the answer is going to depend on when Transylvania transitioned to the Gregorian calendar. :)

At least in the UK, dates in 1752 and earlier won't map directly to the same date in 1753 or later. We lost a whole 11 days in September of 1752 to make the transition.
Andre Oosthuizen 8-Aug-23 9:00am    
Yup, I am sure a few decades or so won't bother him at all. :)

Even in current times I feel like I am loosing a few days every now and then!

Very interesting though, thanks for sharing, learned something today.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900