9 May 2010

C Program to print one month's calender in proper layout

Hi, this is the C code which I wrote to print one month’s calendar. This program takes care of the following situations or failure cases:

  1. It prints the dates as per the usual calendar layout
  2. It takes care of the situation where the month doesn’t start on Sunday.
  3. It takes care of the situation where the user may enter the number of days less than 28 or more than 31.
  4. It takes care of the situation where the user may enter the “start day” number less than 1 or more than 7.

The C code is given below


/*
C Program to print one month's calender in proper format
Programmed By: Amit Biswal
URL : www.123mylist.com 
*/
#include <stdio.h>
#include <conio.h>

int main()
{
    int i,days,start;
    do{
        printf("Enter the number of days\n");
        scanf("%d",&days);
    }while(days<28 | days>31);
    do{
                printf("\nEnter the start day number 1-Sun; 2-Mon; ... ; 7-SAT");
                scanf("%d",&start);
                    
    }while(start<0 | start>7);
    printf("\nSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\n");
    for(i=1;i<=days+start-1;i++)
    {
                              if(i<start)
                              {
                                       printf(" \t");
                              }
                              else
                              {
                                        printf("%d\t",i-start+1);
                              }
                              if((i%7)==0)
                              printf("\n");
    }
    
    
    getch();
    return 0;
}




Related Post :


1 comment:

  1. Hi. I need help with this calendar code. The alignment of the dates are not correct. For example, I have 28 days in a month and my month started on Tuesday, and it printed as follows:

    S M T W T F S
    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

    How can I print only 7 days in a week?? Please help me fix this as I follow exactly what your code are.

    Thanks.

    ReplyDelete

If you liked this blog then Join me at:
Facebook
Twitter
RSS