Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Write a program that reads your birthday from the console as text and
- * prints how old you are now and how old you will be after 10 years.
- * Input: 12.04.1991
- * Output: Now: 24
- * After 10 years: 34
- */
- #include <stdio.h>
- #include <time.h>
- int main()
- {
- int birthDay, birthMonth, birthYear;
- printf("Enter your Birth Date exactly in this format [DD.MM.YYYY]: ");
- if (scanf("%d.%d.%d", &birthDay, &birthMonth, &birthYear) == 1)
- {
- printf("Error! - Invalid input!\n");
- return 1;
- }
- time_t today = time(NULL);
- struct tm * timeNow = localtime(&today);
- int currentYear = timeNow -> tm_year + 1900;
- int currentMonth = timeNow -> tm_mon + 1;
- int currentDay = timeNow -> tm_mday;
- int age = currentYear - birthYear;
- if (birthMonth < currentMonth)
- {
- printf("Now: %d\nAfter 10 years: %d\n", age, age + 10);
- }
- else if (birthMonth == currentMonth)
- {
- if (birthDay <= currentDay)
- {
- printf("Now: %d\nAfter 10 years: %d\n", age, age + 10);
- }
- else
- {
- printf("Now: %d\nAfter 10 years: %d\n", age - 1, age + 9);
- }
- }
- else
- {
- printf("Now: %d\nAfter 10 years: %d\n", age - 1, age + 9);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement