06/04/2016

C Program Date Validation Using If Else Statements

Problem :- Write A C Program To Check Given Date Month And Year Is Correct or Not Using If Else Statements .

Logic :- Check All the Month And Days valid or not also Special Condition February Month And Check for Leap Year Calendar .So basically we need to check all date (day : month :year ) as we know that all days are lies between 28 to 31 days and 12 month in a year or year we need to check .This Program Will Help you A lot See this C++ Program To Check Year Is Leap Year Or Not

below Solution is Sort Compare to This Solution you can see

Try Yourself  C++ Program To Check Date Validation (Valid Or Not)

Solution :-

#include<stdio.h>
#include<conio.h>
void main()
{
  //Ghanendra Yadav
    int d,m,y,i,leap=0,temp=0,temp1=0,temp2=0;

    printf("C Program to Date Validation");
    
    while(1)
    {
    printf("\n\nEnter The Date (dd mm yyyy) : ");
    scanf(" %d %d %d",&d,&m,&y);

    if(y<1600||y>2100)
    {
    printf("\nYear Is Not Valid !!!! Try Again");
    temp=1;
    }
    else
    {
    printf("\nYear Is Valid ");
    }

    if(temp!=1)
    {
    if(y%4==0)
    leap=1;
    }

    if(m<1||m>12)
    {
    printf("\nMonth Is Not Valid !!!! Try Again");
    }
    else
    {
    printf("\nMonth Is Valid ");
    }

    if(d<1||d>31)
    {
    temp2=1;
    }

    if(m>=1&&m<=7)
    {
    for(i=1;i<=6;i++)
    {
      if(m%2==0)
      temp1=30;
      if(m%2==1)
      temp1=31;
    }
    }

    if(leap==0&&m==2)
    {
temp1=28;
}

    if(leap==1&&m==2)
    {
temp1=29;
}

    if(m>=8&&m<=12)
    {
    for(i=7;i<=12;i++)
    {
      if(m%2==0)
      temp1=31;
      if(m%2==1)
      temp1=30;
    }
    }
    if(d>temp1)
    temp2=1;
    if(temp2==1)
    printf("\nDate Is Not Valid !!!!! Try Again");
    if(temp2==0)
    printf("\nDate Is Valid ");

printf("\n\n\nPress Enter For Again Check Date Validity \n");
    getch();
}
}

Output:-

C Program Date Validation Using If Else Statements

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: