Getting the Total Number of Days in a Month Using If-else and Switch Statements in C#

Given a number N, the task is to write a C# program to print the number of days in the Month N.

Examples:

Input: N = 12
Output: 31

Input: N = 2
Output: 28/29

This task can be done using the following approaches:

1. Using if else statement : Appropriate month number is checked and then number of days in a month is print using else if statement. Number of days are follows :

Month = [1, 3, 5, 7, 8, 10, 12] , Number of days = 31
Month = [2] , Number of days = 28/29
Month = [4, 6, 9, 11] , Number of days = 30

Below is the implementation of the above approach:



Output:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to Print Number of Days
// in a Month using Else If Statement
  
using System;
  
public class GFG{
  
    // Function to print the Number
    // of Days in a N
    static void PrintNumberofDays(int N) 
    {
       // Check for the Ns
        if (N == 1 || N == 3 || N == 5 || N == 7
          || N == 8 || N == 10 || N == 12) {
              Console.Write("31");
        
          
        else if (N == 4 || N == 6 || N == 9 ||
               N == 11) {
               Console.Write("30");
        
          
        else if (N == 2) {
               Console.Write("28/29");
        }
    }
      
    // Driver Code
    static public void Main ()
    {
        int N = 5; // 1 <= N <= 12
        PrintNumberofDays(N);
    }
}

chevron_right


Output:

31

2. Using Switch Condition : Same as Else if , here month number is checked and it is redirected to cases,and then number of days in a month is print. Below is the implementation of the above approach:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to Print Number of Days
// in a Month using Switch Condition
  
using System;
  
public class GFG{
  
    // Function to print the Number
    // of Days in a N
    static void PrintNumberofDays(int N) 
    {
        // Check for the N
        switch(N )
        {
            case 1:
            case 3:
            case 5:     
            case 7:
            case 8:
            case 10:
            case 12:             
                Console.Write("31");
                break;
              
            case 4: 
            case 6:
            case 9:
            case 11:                 
                Console.Write("30"); 
                break;
              
            case 2:
            Console.Write("28/29");
            break;
        }
    }
      
    // Driver Code
    static public void Main ()
    {
        int N = 5; // 1 <= N <= 12
        PrintNumberofDays(N);
    }
}

chevron_right


Output:

31



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.