একজন ব্যক্তির বর্তমান তারিখ এবং জন্ম তারিখ দিয়ে দেওয়া এবং কাজ হল তার বর্তমান বয়স গণনা করা।
উদাহরণ
Input-: present date-: 21/9/2019 Birth date-: 25/9/1996 Output-: Present Age Years: 22 Months:11 Days: 26
নিচে ব্যবহৃত পদ্ধতিটি নিম্নরূপ −
- একজন ব্যক্তির বর্তমান তারিখ এবং জন্ম তারিখ ইনপুট করুন
- শর্তগুলি পরীক্ষা করুন
- যদি বর্তমান মাস জন্ম মাসের চেয়ে কম হয়, তাহলে আমরা বর্তমান বছর বিবেচনা করব না কারণ এই বছরটি এখনও সম্পূর্ণ হয়নি এবং বর্তমান মাসে 12 যোগ করে মাসের পার্থক্য গণনা করতে হবে।
- যদি বর্তমান তারিখটি জন্ম তারিখের চেয়ে কম হয়, তাহলে আমরা মাস বিবেচনা করব না এবং বিয়োগকৃত তারিখগুলি তৈরি করার জন্য বর্তমান তারিখের সাথে মাসের দিনের সংখ্যা যোগ করব এবং ফলাফলের তারিখের মধ্যে পার্থক্য হবে৷
- যখন এই শর্তগুলি পূরণ হয় শুধুমাত্র চূড়ান্ত ফলাফল পেতে দিন, মাস এবং বছর বিয়োগ করুন
- শেষ বয়স মুদ্রণ করুন
অ্যালগরিদম
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 উদাহরণ
#include <stdio.h>
#include <stdlib.h>
// function to calculate current age
void age(int present_date, int present_month, int present_year, int birth_date, int birth_month, int birth_year) {
int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (birth_date > present_date) {
present_date = present_date + month[birth_month - 1];
present_month = present_month - 1;
}
if (birth_month > present_month) {
present_year = present_year - 1;
present_month = present_month + 12;
}
int final_date = present_date - birth_date;
int final_month = present_month - birth_month;
int final_year = present_year - birth_year;
printf("Present Age Years: %d Months: %d Days: %d", final_year, final_month, final_date);
}
int main() {
int present_date = 21;
int present_month = 9;
int present_year = 2019;
int birth_date = 25;
int birth_month = 9;
int birth_year = 1996;
age(present_date, present_month, present_year, birth_date, birth_month, birth_year);
return 0;
} আউটপুট
যদি আমরা উপরের কোডটি চালাই তবে এটি নিম্নলিখিত আউটপুট তৈরি করবে
Present Age Years: 22 Months:11 Days: 26