calendar program odd days method

                        

 

          calendar program odd days method

          first visit code first fall understand odd method please flow my simple example

             Remember  this two logics months odd code and year odd code

year code

 code                  days                                                                0 odd days for

 0                      Sunday                                                     those are 2000,1600,400 years
1                       Monday                                                   it will contained 0 odd days
2                       Tuesday                                                     
3                       Wednesday                                           for 100 years 5 odd days
4                       Thursday                                             for 365 days 52weeks +1 odd
5                       Friday                                            
6                       Saturday
             month code                                                                               calculation month odd days
no   month                 days

0    January                   31
1    February                28    if  it leap year 29                               feb=28days
2    March                     31                                                                 28/7=odd days
3    April                        30                                                      leap    or 29/7 =1 odd day    
4    May                          31                                               similar to  all January 31/7=3 odd days
5    June                          30                                                                  April        30/7=2 odd days
6    July                            31
7    August                     31
8    September               30
9    October                      31
10    November             30
11    December                31


             example   what is day on 2018 dec 1?
                            
            for  year>>   2018=2000+17                check odd for 2000  and  how many leap year in 18                                                                                  year simply divide by 4 will get 18/4=4  then add to 17
                        year  odd days  = 0+18+4=22/7=1 odd day for year
for month >> dec=12 -1=11    sum all up to nov how much 31+28+30+..30=365-31=334%7=5
  for day  >>>     in my problem 1 then reminder is 1 that why one odd day  if date 25/7=4 odd day 
    now add all odd days =for year+for month+for day
                                          1          +     5        +      1         =7 means Saturday check (0-6)


 please flows code by above example you will understand 

include<stdio.h>
#include<string.h>
int feb(int y)         //    condition for leap year if it leap February 29 other wise 28
{
    int z,a;
    z=y%4;            // condition every four years will come one leap year that divide by 4
    if(z==0)         // after divide the value zero means 4%4 remainder zero
        a=29;
    else
        a=28;
    return a;

}
int  monthdays1(int mnum1)// return no days for month
{    
    int str[]={31,28,31,30,31,30,31,31,30,31,30,31};// days for months
    return str[mnum1];
}
int  monthdays(int mnum,int year)
{
    int res[12]={0},i=0,res1=0;
    int str[]={31,28,31,30,31,30,31,31,30,31,30,31};//days
    if(mnum==1)  // check if its leap year or not its important to February
    {
        str[1]=feb(year);// call feb function
    }
    for(i=0;i<mnum;i++)// sum all month days for find odd days in month
    {
        res[i]=res[i]+str[i]%7;// it will assigned every month odd days to that month
    }
    for(i=0;i<=mnum;i++)
    {
        res1=res1+res[i];   //sum all odd days
    }
    int r=(res1%7+1);// I am one because I am assuming date 1 st
    return r;// it will return value

}
int  display(int current,int month1,int year)// it will display output
{
    int i=0,k=0,l=0,num=0;   
    char *str[]={"sun","mon","tue","wed","thu","fri","sat"};// week days
    for(i=0;i<=strlen(str);i++)
    {
        printf("%s\t",str[i]);// it will print week days

    }
    printf("\n");

    for(i=0;i<current;i++)//current for spaces for start calendar means above example 7  
                                                              means Saturday start from 6 postion in 0 th row
    {
        printf(" \t");
    }
    if((month1+1)==2)//check if its leap year or not its important to February 

    {
        num=feb(year);
    }
    else
    {
        num=monthdays1(month1);
    }
    for(i=1;i<=(7-current);i++)//if current 7 it will break other wise tap spaces print
    {
        printf("%d\t",i);
    }
    int j=i;              //it will continues the days  like if current end the 0 th row then you need print next
    printf(" \n");                  row elements
    for(l=1;l<=5;l++)//        in calendar 5 rows 7 colomums
    {
        for(k=1;k<=7;k++)
        {
            if(j==num+1)             // if days 31or 30 then it will break loop
                return;
            printf("%d\t",j);
            j++;
        }
        printf("\n");
        if(j==num)
            break;
    }
}           

int main()
{       
    int k,year,year1600,year400,year4,year5,year2000;
    int n;
    int   odd=0 ,odd1=0,odd2=0,odd3=0,odd4=0;
    printf("enter any year\n");
    scanf("%d",&year);
    if(year>=2000)           //  if you enter more than 2000 year it allows
    {
        year2000=year-2000;
        odd4=0;
        year400=year2000;    // assigned values further process
        year1600=year2000;
    }
    if(year>=1600)
    {         year1600=year-(1600+1);//   why put +1 here because 2018 means you will calculate 2017
        odd1=0;                                      //see above example
    }
    if(year1600>=400)                     // if value more than 400 year
    {         year400=year1600-400;
        odd2=0;
    }
    else                                          // for all 400,1600,2000 odd days zero but reaming not zero
    {    
        int y500=year1600;                     //if 300 year odd days will 15 days
        year400=year1600/100;                //          300/100=3*5=15 how for 100 years 5 odd days
        year400=year400*5;
        odd2=year400%7;
        year400=y500%100;
    }
    if(year400>=4)                      // after reaming value 17 check 17+4
    {
        odd3=year400+year400/4;
    }

    odd=odd1+odd2+odd3+odd4;
    printf("year odd days %d\n",odd%7);
    printf("enter any mon\n");
    scanf("%d",&n);
    int res =monthdays(n-1,year);
    printf(" month odd %d\n",res);
    k=(res+odd)%7;
    printf(" current %d\n",k);
    display(k,n-1,year);

}


Comments

Popular posts from this blog

circular linked list

C in Depth