1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Calendar in Plain C

Discussion in 'C' started by shabbir, Oct 9, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Here is the code which displays the Calendar of the month entered by the user.

    Code:
    #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
    
     

    Attached Files:

  2. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    This code is not showing the dates. why? there is no error at compile time or runtime.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    It shows. Just debug it out if it says the printf with some correct values.
     
  4. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    i tried it works
     
  5. sura

    sura Banned

    Joined:
    Aug 4, 2011
    Messages:
    47
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    India,Tamil Nadu.
    in the display function the second for loop for printing the numbers is not working
    the numbers ain't showing...
    can someone help me.
     
  6. mukeshsoftona

    mukeshsoftona Banned

    Joined:
    Oct 28, 2011
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    dear i am confused using loops. anybody help me .
     

Share This Page