Find Number of Days in a Month

Duration: 4 min

This video lesson is available to enrolled students.

Enroll to watch — TCS Test Series

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This video is a programming tutorial from Knowledge Gate that teaches how to write a C program to determine the number of days in a given month. The instructor first presents the problem statement, Q10, which requires a user to input a date in dd-mm-yyyy format and output the number of days in that month, with the instruction to ignore leap years. The solution is broken down into a logical approach: parsing the input string to extract the month, using a predefined array of days for each month, and then outputting the result. The instructor then transitions to writing the code, showing the necessary header files, the main function, and the use of the `strtok` function to split the date string by the '-' delimiter. The code is completed by converting the month string to an integer and using it as an index to access the correct number of days from the array. The video concludes with the final code snippet and a brief explanation of the logic.

Chapters

  1. 0:00 2:00 00:00-02:00

    The video begins with the Knowledge Gate logo, followed by a slide introducing the problem, Q10. The problem statement is clearly displayed: 'User enters date in dd-mm-yyyy find out number of days in month (for now please ignore the concept of leap year)'. The input is specified as a date in dd-mm-yyyy format, and the output is the number of days in that month. Two public test cases are provided: for the date 12-03-2006, the output is 31, and for 31-11-1996, the output is 30. The instructor, Yash Jain Sir, is visible in a small window, and the slide includes a 'Placement Expert' graphic and a copyright notice for Knowledge Gate Eduventures. The instructor begins to explain the problem, emphasizing the need to parse the date string to extract the month.

  2. 2:00 4:18 02:00-04:18

    The instructor transitions to the coding phase, with a 'Code' label appearing on the screen. He begins writing the C program, starting with the necessary header files: #include <stdio.h> and #include <string.h>. He then defines the main function and initializes an integer array `arr` with the number of days for each month, starting from January (index 0). The array is populated as {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}. He then writes the code to take user input using `scanf`, store it in a character array `date[11]`, and use `strtok` to split the string by the '-' delimiter to extract the month. He explains that the month string is then converted to an integer using `atoi` and used as an index to access the correct number of days from the array, which is printed as the output. The final code snippet is shown on the screen, and the instructor explains the logic of the program.

The video provides a clear, step-by-step tutorial on solving a common programming problem. It starts with a well-defined problem statement and test cases, then logically breaks down the solution into parsing the input, using a data structure (an array) to store the number of days per month, and finally writing the code to implement this logic. The progression from problem definition to code implementation is methodical and educational, making it a useful resource for learning basic string manipulation and array usage in C.

Loading lesson…