Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Calendar in Plain C (http://www.go4expert.com/articles/calendar-plain-c-t1581/)

shabbir 9Oct2006 14:32

Calendar in Plain C
 
1 Attachment(s)
Here is the code which displays the Calendar of the month entered by the user.

Code: C

#include<stdio.h>
#include<conio.h>

#define Y 1900             /*1/1/1900 monday*/

void days(int *,int *);
void display(int *, int);

int _MONTH[]={{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};

void main()
{
    char ch = 'y';
    int m,yr;
    do
    {
        printf("\nEnter month [1-12] ");
        scanf("%ld",&m);
        printf("Enter year ");
        scanf("%ld",&yr);
        if((yr%4==0)&&((yr%100!=0)||(yr%400==0)))
            _MONTH[1]=29;
        days(&m,&yr);
        printf("\n\n\n\nPress 'x' to EXIT\n");
        fflush(stdin);
        ch = getch();
    }while(ch != 'x');
}

void days(int *m,int *yr)
{
    int i,md=0,leap=0,track;
    unsigned int d,yrd;
    for(i=Y;i<*yr;i++)
    {
        if((i%4==0)&&((i%100!=0)||(i%400==0)))
            leap++;
    }
    for(i=0;i<(*m-1)&&(i<11);i++)
        md=md+_MONTH[i];
    yrd=(*yr-Y)*365;
    d=yrd+leap+md;
    track=d%7;
    display(&track,*m);
}

void display(int *track,int m)
{
    int t,dt,loop;
    t=*track;
    printf("\n\n\n\t\t");
    printf("MON\tTUES\tWED\tTHURS\tFRI\tSAT\tSUN\n\n");

    for(loop=0;loop<t+2;loop++) /*t+2 due to two additional \t*/
        printf("\t");

    for(dt=1;dt<=_MONTH[m-1];dt++)
    {
        if(t%7==0 && t!=0)
            printf("\n\n\t\t");
        printf("%d",dt);
        printf("\t");
        t++;
    }
}

Test Run output
Code:

Enter month [1-12] 12
Enter year 1979



                MON    TUES    WED    THURS  FRI    SAT    SUN

                                                        1      2

                3      4      5      6      7      8      9

                10      11      12      13      14      15      16

                17      18      19      20      21      22      23

                24      25      26      27      28      29      30

                31



Press 'x' to EXIT


Bhullarz 17Nov2006 09:47

Re: Calendar in Plain C
 
This code is not showing the dates. why? there is no error at compile time or runtime.

shabbir 17Nov2006 10:25

Re: Calendar in Plain C
 
It shows. Just debug it out if it says the printf with some correct values.

aisha.ansari84 5Mar2008 18:47

Re: Calendar in Plain C
 
Quote:

Originally Posted by Bhullarz
This code is not showing the dates. why? there is no error at compile time or runtime.

i tried it works

sura 27Oct2011 05:25

Re: Calendar in Plain C
 
in the display function the second for loop for printing the numbers is not working
the numbers ain't showing...
can someone help me.

mukeshsoftona 28Oct2011 18:12

Re: Calendar in Plain C
 
dear i am confused using loops. anybody help me .


All times are GMT +5.5. The time now is 14:21.