-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBirthday age calculation
53 lines (49 loc) · 1.83 KB
/
Birthday age calculation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <time.h>
// A function to calculate the current date
void get_current_date(int *current_day, int *current_month, int *current_year)
{
// Get the current time
time_t t = time(NULL);
// Convert it to a structure that contains date and time information
struct tm tm = *localtime(&t);
// Assign the values to the pointers
*current_day = tm.tm_mday;
*current_month = tm.tm_mon + 1;
*current_year = tm.tm_year + 1900;
}
// A function to calculate the age of a person by using their birth date
int calculate_age(int birth_day, int birth_month, int birth_year)
{
// Declare variables to store the current date
int current_day, current_month, current_year;
// Call the function to get the current date
get_current_date(¤t_day, ¤t_month, ¤t_year);
// Declare a variable to store the age
int age = current_year - birth_year;
// Adjust the age if the person has not had their birthday yet this year
if (birth_month > current_month || (birth_month == current_month && birth_day > current_day))
{
age--;
}
// Return the age
return age;
}
// The main function of the program
int main()
{
// Declare variables to store the name and birth date of the person
char name[50];
int birth_day, birth_month, birth_year;
// Prompt the user to enter their name and birth date
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your birth date (dd/mm/yyyy): ");
scanf("%d/%d/%d", &birth_day, &birth_month, &birth_year);
// Call the function to calculate the age of the person
int age = calculate_age(birth_day, birth_month, birth_year);
// Display the name and age of the person
printf("Hello %s, you are %d years old.\n", name, age);
// Return 0 to indicate successful execution
return 0;
}