For this question: Write a C program that determines the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31, 1996, is day 366, since 1996 is a leap year. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 400. Your program should accept the month, day, and year as integers. Include a function leap that returns 1 if called with a leap year, 0 otherwise. Example running the code: Please enter day [1=31], month [1-12], and year [e.g. 2020] as integers: 12 2 2021 Output: Day Number: 43
For this question:
Write a C program that determines the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31, 1996, is day 366, since 1996 is a leap year. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 400. Your program should accept the month, day, and year as integers. Include a function leap that returns 1 if called with a leap year, 0 otherwise.
Example running the code:
Please enter day [1=31], month [1-12], and year [e.g. 2020] as integers:
12 2 2021
Output:
Day Number: 43
I wrote this code but whenever I run it and enter the day, month, and year values, it returns blank.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int leap_year(int year);
int main()
{
int day,month,year;
printf("Enter the date:(DD MM YYYY)\n");
scanf("%d%d%d",&day,&month,&year);
if ((day>31 || day<1) || (month<1 || month>12))
printf("Error! Invalid day or month.\n");
else
{
switch(month)
{
case '1':
printf("Day Number: %d.\n",day);
break;
case '2':
if ((leap_year(day)==1 && day>29)|| (leap_year(day)==0 && day>28))
printf("Error! Invalid day.\n");
else
printf("Day Number: %d.\n",day+31);
break;
case '3':
if (leap_year(year)==1)
printf("Day Number: %d.\n",day+60);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+59);
break;
case '4':
if (day>30)
printf("Error! Invalid day.\n");
else if (leap_year(year)==1)
printf("Day Number: %d.\n",day+91);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+90);
break;
case '5':
if (leap_year(year)==1)
printf("Day Number: %d.\n",day+121);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+120);
break;
case '6':
if (day>30)
printf("Error! Invalid day.\n");
else if (leap_year(year)==1)
printf("Day Number: %d.\n",day+152);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+151);
break;
case '7':
if (leap_year(year)==1)
printf("Day Number: %d.\n",day+182);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+181);
break;
case '8':
if (leap_year(year)==1)
printf("Day Number: %d.\n",day+213);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+212);
case '9':
if (day>30)
printf("Error! Invalid day.\n");
else if (leap_year(year)==1)
printf("Day Number: %d.\n",day+244);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+243);
break;
case '10':
if (leap_year(year)==1)
printf("Day Number: %d.\n",day+274);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+273);
break;
case '11':
if (day>30)
printf("Error! Invalid day.\n");
else if (leap_year(year)==1)
printf("Day Number: %d.\n",day+305);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+304);
break;
case '12':
if (leap_year(year)==1)
printf("Day Number: %d.\n",day+335);
else if (leap_year(year)==0)
printf("Day Number: %d.\n",day+334);
break;
}
}
}
int leap_year(int year)
{
int leap_day;
if
(year%100==0 && year%400==0)
{leap_day=1;
return leap_day;}
else if(year%4==0)
{leap_day=1;
return leap_day;}
else
leap_day=0;
return leap_day;
}
Any idea why? Please use if statements and switch cases and not arrays.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images