Write a C program
Sample input/output
INCOMPLETE CODE
#include <stdio.h>
#define N 3
int main(){
char input[30];
for(i=0; i<N;i++)
{
printf(“Enter month day and year separated by dash: ” );
fgets(input, 30, stdin); /* read in the whole line */
/* manipulate the input, count days and display */
}
return 0;
}
int countDays(int m, int d){
}
Write an ANSI-C pragram that reads input from the standard input about date, and then calculate the number days that has elapsed in the year. The program runs a loop for pre-defined N times. In each interaction, the user is prompted to enter Month-Day Year (three words separated by a dash and a space). For each input your program displays the approximate days that have elapsed from the start of the year.
Expert Answer
Code :
#include <stdio.h>
#define N 3
int main(){
char input[30];
int i, j=0;
int day=0, month=0, year =0 ;
int daycount=0;
for(i=0; i<N;i++)
{
printf(“n Enter month day and year separated by dash: ” );
fgets(input, 30, stdin); /* read in the whole line */
day =0; month =0; year = 0; j =0; daycount =0;
/* manipulate the input, count days and display */
// manipulating date
while(input[j]!=’-‘){
month = month*10 + (input[j]-48);
j++;
}
j++;
while(input[j]!=’ ‘){
day = day*10 + (input[j]-48);
j++;
}
j++;
while(input[j]>=48 && input[j] <= 58){
year = year*10 + (input[j]-48);
j++;
}
printf(“n Day : %d, month: %d, year: %d”, day, month, year);
daycount = countDays(month, day);
printf(“n Approximately %d days of year %d have elapsed.n”, daycount, year);
}
return 0;
}
int countDays(int m, int d){
int i =0;
int dayCount=0;
for(i=1 ; i<m; i++){
if(i==1 || i==3 || i==5 || i==7 || i==9 || i==11){
dayCount = dayCount + 31;
}
else if(i==2){
dayCount = dayCount + 28;
}
else{
dayCount = dayCount+30;
}
}
dayCount = dayCount + d;
return dayCount;
}
Output :