Given a number N, the task is to find the number of days corresponding to each month where 1 is January, 2 is February, 3 is March, and so on.
Examples:
Input: N = 12
Output: 31 Days
Input: N = 2
Output: 28/29 Days
Method – 1: using If Else:
- Get the input month as a number N.
- If N is one of these value 1, 3, 5, 7, 8, 10, 12, then print “31 Days.”.
- If N is one of these value 4, 6, 9, 11, then print “30 Days.”.
- If N is 2, then print “28/29 Days.”.
- Else print “Invalid Month”.
Below is the implementation of the above approach:
#include <stdio.h>
void printNumberOfDays( int N)
{
if (N == 1 || N == 3 || N == 5
|| N == 7 || N == 8 || N == 10
|| N == 12) {
printf ( "31 Days." );
}
else if (N == 4 || N == 6
|| N == 9 || N == 11) {
printf ( "30 Days." );
}
else if (N == 2) {
printf ( "28/29 Days." );
}
else {
printf ( "Invalid Month." );
}
}
int main()
{
int N = 4;
printNumberOfDays(N);
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method – 2: using Switch Statements:
- Get the input month as a number N.
- Using switch statement when value of N is one of 1, 3, 5, 7, 8, 10, 12, then print “31 Days.” corresponding to switch case.
- If N is one of these value 4, 6, 9, 11, then print “30 Days.” corresponding to switch case.
- If N is 2, then print “28/29 Days.” corresponding to switch case.
- Else the default condition for the switch case will print “Invalid Month”.
Below is the implementation of the above approach:
#include <stdio.h>
void printNumberOfDays( int N)
{
switch (N) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf ( "31 Days." );
break ;
case 4:
case 6:
case 9:
case 11:
printf ( "30 Days." );
break ;
case 2:
printf ( "28/29 Days." );
break ;
default :
printf ( "Invalid Month." );
break ;
}
}
int main()
{
int N = 4;
printNumberOfDays(N);
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method – 3: using Arrays:
- Store the value of days corresponding to each month in an array as:
arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
- Print the corresponding day to each month from the above array.
Below is the implementation of the above approach:
#include <stdio.h>
int main()
{
int arr[12] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int N = 4;
printf ( "%d Days." , arr[N - 1]);
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method – 4: using Pointers:
- Store the value of days corresponding to each month in an array as:
arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
- Print the corresponding day to each month from the above array using pointers as:
printf(“%d Days.”, *(arr + (*N – 1)))
Below is the implementation of the above approach:
#include <stdio.h>
void printNumberOfDays( int * arr, int * N)
{
printf ( "%d Days." , *(arr + (*N - 1)));
}
int main()
{
int arr[12] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int N = 4;
printNumberOfDays(arr, &N);
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Jul, 2020
Like Article
Save Article