Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please I need help with this c programming question As you get older, it gets ha

ID: 3667144 • Letter: P

Question

please I need help with this c programming question As you get older, it gets harder to do math in your head. But in your current job, you must routinely determine a persons age relative to the current date. To make your life easy you decide to write a C program that will take a persons birthday as input, and output back a sentence like the following: You were born on Apr 4, 2000 and you are 15 years old. The birthday should be entered as 3 separate fields (year, month, day), and the input must be checked for validity. The month should be input as an integer in the range 1..12, and then output back as its 3 character abbreviation. You may assume that the current day is hard-coded to Feb 12, 2016 (i.e., you do not prompt for the current date). If an invalid date has been entered you must produce the following output. An invalid birth date has been entered. To simplify the checking, you may assume that every year divisible by 4 is a leap-year. The following are examples of invalid birth dates: 2000 2 30 (not 30 days in Feb) ,1997 2 29 (not a leap year) ,2016 2 17 (date in future) ,2011 13 7 (13 is not a valid month) . 2000 7 -2 (-2 is not a valid day). please the program should compile successfully with the command: gcc -Wall -std=c99 -lm your file.c

Explanation / Answer


// header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

// define main function
void main(int argc, char *argv[])
{

// getting year from user
int year = 0;
printf("Enter the year in which you were born: ");
scanf("%d", &year);

// getting month from user
int month = 0;
printf("Enter the month in which you were born (1-12): ");
scanf("%d", &month);

   // getting day from user
int day = 0;
printf("Enter the day of the month in which you were born (1-31): ");
scanf("%d", &day);

// declare variables
char str[100];
time_t now = time(NULL);
struct tm *ptrNow;
struct tm bday;
bday.tm_mday = day;
bday.tm_mon = month - 1;
bday.tm_year = year % 100;
bday.tm_sec = 0;
bday.tm_min = 0;
bday.tm_hour = 0;
ptrNow = localtime(&now);
strftime(str,80,"%Y/%m/%d",ptrNow);
printf("Today is %s ", str);
strftime(str,80,"%Y/%m/%d",&bday);
printf("Your birthday was %s ", str);

time_t bdayT = mktime(&bday);
long diff = (long) difftime(now, bdayT);

// printf("You are %d second old ", diff);
int years = diff / (365.25 * 24 * 60 * 60);
diff -= years * (365.25 * 24 * 60 * 60);
int days = diff / (24 * 60 * 60);

int weeks = days / 7;
days = days % 7;

// display output
printf("You are %d years, %d weeks, and %d days old today ", years, weeks, days);

}

Sample Output

                                                                                                                                               
Enter the year in which you were born: 1988                                                                                                                 
Enter the month in which you were born (1-12): 6                                                                                                            
Enter the day of the month in which you were born (1-31): 17                                                                                                
Today is 2016/02/11                                                                                                                                         
Your birthday was 1988/06/17                                                                                                                                
You are 27 years, 34 weeks, and 0 days old today