C Lab1
C Lab1
Submitted by Submitted to
Rashed Ahmed Hridoy Samin Yasar
ID: 20245103289 Department of Computer Science
and Engineering
Intake: 53
Section: 8
Department of CSE
Code:
#include<stdio.h>
int main()
{
int d,m,y;
printf("Enter day: ");
scanf("%d",&d);
printf("Enter month: ");
scanf("%d",&m);
printf("Enter year: ");
scanf("%d",&y);
if(d>0 && d<32 && m>0 && m<13)
{
if(m==2)
{
if((y%4==0 && y%100!=0)||y%400==0)
{
if(d<30)
printf("Date is valid");
else
printf("Date is not valid");
}
else
{
if(d<29)
printf("Date is valid");
else
printf("Date is not valid");
}
}
else if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
{
if(d<32)
printf("Date is valid");
else
printf("Date is not valid");
}
else
{
if(d<31)
printf("Date is valid");
else
printf("Date is not valid");
}
}
else
printf("Date is not valid");
return 0;
}
Explanation:
This a program that checks if the date entered by the user is valid or not.
The program starts by asking the user to input a day (d), month (m), and year (y).
It then checks if the day is between 1 and 31 and the month is between 1 and 12.
If not, it prints "Date is not valid" and ends.
If the month is February (m==2), it checks if the year is a leap year. A leap year is
either divisible by 400 or divisible by 4 but not by 100. If it is a leap year, it checks if
the day is less than or equal to 29. If it's not a leap year, it checks if the day is less
than or equal to 28. If these conditions are met, it prints "Date is valid", else it prints
"Date is not valid".
If the month is one that has 31 days (January, March, May, July, August, October, or
December), it checks if the day is less than or equal to 31. If not, it prints "Date is
not valid".
If the month is one that has 30 days (April, June, September, or November), it checks
if the day is less than or equal to 30. If not, it prints "Date is not valid".
If one of the above conditions is met, it prints "Date is valid".