221k views
0 votes
First read current date(day,month, year) from the monitor and then in a loop read name and birth date(day month year) of a person from the monitor and find and display his/her name and age in terms of day month and year.

Write A C Program using the following assumptions. Use actual days for each month. Months 4, 6 , 9 , 11 are 30 days, month 2 is 28 days and the others months are 31 days.
Example data could be as follows 22 3 2023  current day
AHMET 12 08 1995
GULEN 27 03 2001
MERT 18 12 1999
EOF(Ctl+Z) using keyboard

1 Answer

3 votes

Final answer:

To solve this problem, you need to write a C program that takes the current date as input and then uses a loop to read the name and birth date of a person. The program should calculate the person's age in terms of days, months, and years and display the result.

Step-by-step explanation:

To solve this problem, you need to write a C program that takes the current date as input and then uses a loop to read the name and birth date of a person. The program should calculate the person's age in terms of days, months, and years and display the result.

You can use the assumptions provided in the question to determine the number of days in each month. For example, if the current month is April (month number 4), you can set the number of days to 30.

Here's a basic example of how the program might look:

#include <stdio.h>
#include <time.h>

int main() {
int currentDay, currentMonth, currentYear;
time_t t = time(NULL);
struct tm tm = *localtime(&t);

// Read current date
printf("Enter current date (day month year): ");
scanf("%d %d %d", ¤tDay, ¤tMonth, ¤tYear);

// Read names and birth dates
printf("Enter names and birth dates (day month year), enter EOF to stop: ");
while (scanf("%s %d %d %d", name, &day, &month, &year) == 4) {
// Calculate age
int ageYears = currentYear - year;
int ageMonths = currentMonth - month;
int ageDays = currentDay - day;

// Adjust for negative age
if (ageDays < 0) {
ageMonths--;
ageDays += 30;
}
if (ageMonths < 0) {
ageYears--;
ageMonths += 12;
}

// Display result
printf("%s: %d years, %d months, %d days\\", name, ageYears, ageMonths, ageDays);
}

return 0;
}

User Egprentice
by
8.5k points