Wednesday, 2 June 2021

How to calculate adult age in C language while loop?

Given accepted date and bearing date, acquisition the present age. Examples: Input : Bearing date = 07/09/1996 Present date = 07/12/2017 Output : Present Age = Years: 21 Months: 3 Days: 0 t Age = Years: 7 Months: 11 Days: 21 Recommended: Please try your access on {IDE} first, afore affective on to the solution. While artful the aberration in two dates we charge to aloof accumulate clue of two altitude that will do. If the accepted date is beneath than that of the bearing date, again that ages is not counted, and for abacus dates we add cardinal of ages canicule to the accepted date so as to get the aberration in the dates. If the accepted ages is beneath than the bearing month, again the accepted year is not taken into calculation as this year has not been completed and for accepting the aberration of months, we decrease by abacus 12 to the accepted month. At the end we aloof charge to decrease the days, months and years to get the aberration afterwards the two altitude are dealt with. Below is the accomplishing of the aloft access : // c program for age calculator #include #include // function to calculate current age void findAge(int current_date, int current_month, int current_year, int birth_date, int birth_month, int birth_year) { // days of every month int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // if birth date is greater then current birth // month then do not count this month and add 30 // to the date so as to subtract the date and // get the remaining days if (birth_date > current_date) { current_date = current_date + month[birth_month - 1]; current_month = current_month - 1; } // if birth month exceeds current month, then do // not count this year and add 12 to the month so // that we can subtract and find out the difference if (birth_month > current_month) { current_year = current_year - 1; current_month = current_month + 12; } // calculate date, month, year int calculated_date = current_date - birth_date; int calculated_month = current_month - birth_month; int calculated_year = current_year - birth_year; // print the present age printf("Present Age\nYears: %d Months: %d Days:" " %d\n", calculated_year, calculated_month, calculated_date); } // driver code to check the above function int main() { // current dd// mm/yyyy int current_date = 7; int current_month = 12; int current_year = 2017; // birth dd// mm// yyyy int birth_date = 16; int birth_month = 12; int birth_year = 2009; // function call to print age findAge(current_date, current_month, current_year, birth_date, birth_month, birth_year); return 0; } Output: Present Age Years: 7 Months: 11 Days: 22

No comments:

Post a Comment

C Programming

What is DBMS in brief?

A Database Management System (DBMS) is a software suite designed to efficiently manage, organize, store, manipulate, and retrieve data. It a...