Computer Science, asked by shauryamishra0808, 12 hours ago

2. Write a program to calculate the age of a person. ​

Answers

Answered by riddhibbhoite
1

Answer:

-Input the current date and birth date of a person

-Check for the conditions

-If current month is less than the birth month, then we will not consider the current year because this year has not been completed yet and to compute the differences in months by adding 12 to the current month.

-If the current date is less than the birth date, then we will not consider month and for generating subtracted dates add number of month days to the current date and the result will difference in the dates.

-When this conditions are meet just subtract the days, months and year to get the final result

-Print the final age

Answered by Krtisha
0
Approach used below is as follows −

Input the current date and birth date of a person
Check for the conditions
If current month is less than the birth month, then we will not consider the current year because this year has not been completed yet and to compute the differences in months by adding 12 to the current month.
If the current date is less than the birth date, then we will not consider month and for generating subtracted dates add number of month days to the current date and the result will difference in the dates.
When this conditions are meet just subtract the days, months and year to get the final result
Print the final age

Algorithm
Start
Step 1-> declare function to calculate age
void age(int present_date, int present_month, int present_year, int birth_date, int birth_month, int birth_year)
Set int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
IF (birth_date > present_date)
Set present_date = present_date + month[birth_month - 1]
Set present_month = present_month – 1
End
IF (birth_month > present_month)
Set present_year = present_year – 1
Set present_month = present_month + 12
End
Set int final_date = present_date - birth_date
Set int final_month = present_month - birth_month
Set int final_year = present_year - birth_year
Print final_year, final_month, final_date
Step 2-> In main()
Set int present_date = 21
Set int present_month = 9
Set int present_year = 2019
Set int birth_date = 25
Set int birth_month = 9
Set int birth_year = 1996
Call age(present_date, present_month, present_year, birth_date, birth_month,
birth_year)
Stop
Similar questions