Wednesday, September 19, 2018

Month Calculator

  • Problem Description

    The length of a month varies from 28 to 31 days. In this exercise you will create a program that reads the name of a month from the user as a string. Then your program should display the number of days in that month. 

    Display 28 or 29 days for February so that leap years are addressed. 

    The program should get input from Jan to Dec. 

    If the input is from may to other months then display error messages as "Invalid"
  • CODING ARENA
  • #include <stdio.h>
    #include <string.h>
    int main()
    {
      char month[100],a[]="Jan",b[]="Feb",c[]="Mar",d[]="Apr",e[]="May",f[]="Jun",g[]="Jul",h[]="Aug",i[]="Sep",j[]="Oct",k[]="Nov";
      scanf("%s",month);
      if(strcmp(month,a)==0)
        printf("31");
      else if(strcmp(month,b)==0)
        printf("28 or 29");
      else if(strcmp(month,c)==0)
        printf("31");
      else if(strcmp(month,d)==0)
        printf("30");
      else if(strcmp(month,e)==0)
        printf("31");
      else if(strcmp(month,f)==0)
        printf("30");
      else if(strcmp(month,g)==0)
        printf("31");
      else if(strcmp(month,h)==0)
        printf("31");
      else if(strcmp(month,i)==0)
        printf("30");
      else if(strcmp(month,j)==0)
        printf("31");
      else if(strcmp(month,k)==0)
        printf("30");
      else
        printf("31");
      return 0;
    }
  • Test Case 1

    Input (stdin)
    Jun
    
    
    Expected Output
    30
  • Test Case 2

    Input (stdin)
    Feb
    
    
    Expected Output
    28 or 29

No comments:

Post a Comment