I'm supposed to make a program that will tell me the number of the day of a certain date. So I input the date, and it will tell me which number day it is. (i.e: 1.1.2014 would be day 1, and 30.4.2012 would be 121) The program in its current state works, but it looks a little sloppy (or I'm just too self-conscious). I'm somewhat new to programming and was wondering if I can improve it, or if I can make it less sloppy.
#include <stdio.h>
#include <stdlib.h>
valid(int day, int month, int year)
{
int st[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int copy[12]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(year % 4 != 0)
{
if(day < 1 || day > st[month-1] || month < 1 || month > 12 || year < 0)
return 0;
else
return 1;
}
else
if(day < 1 || day > copy[month-1] || month < 1 || month > 12 || year < 0)
return 0;
else
return 1;
}
int main()
{
int day, month, year, sum;
scanf("%d", &day);
scanf("%d", &month);
scanf("%d", &year);
printf("Date is %d.%d.%d", day, month, year);
sum = day;
int st[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int copy[12]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(valid(day, month, year) == 1)
{
if(year % 4 == 0)
for(int i = 0; i < month; i++)
sum = sum + copy[i-1];
else
for(int i = 0; i < month; i++)
sum = sum + st[i-1];
printf("\nDay number is %d", sum);
}
else
printf("\nInvalid date");
}
The function valid
checks whether my date is valid, so that I cannot input a wrong date. I also check for leap years inside my main program. I have an array which has the number of days in every month, and another one for leap years.
valid()
declaration. \$\endgroup\$