w3resource

C#: Print the name of the first three letters of month of a year starting form current date

C# Sharp DateTime: Exercise-50 with Solution

Write a C# Sharp program to print the name of the first three letters of each month of a year starting from current date.

Sample Solution:-

C# Sharp Code:

using System; // Importing the System namespace

class dttimeex50 // Declaring a class named dttimeex50
{
    static void Main() // Declaring the Main method
    {
        Console.Write("\n\n Display the name of the first three letters of month of a year :\n"); // Displaying a message in the console
        Console.Write("---------------------------------------------------------------------\n"); // Displaying a separator line
        DateTime now = DateTime.Now; // Initializing a DateTime object with the current date and time
        Console.WriteLine(" The date of Today : {0}",now.ToString("dd/MM/yyyy")); // Displaying the current date in the specified format
        Console.WriteLine(" The twelve months are :"); // Displaying a message about the twelve months
        for (int i = 0; i < 12; i++) // Looping through 12 months
        {
            Console.WriteLine(" {0}",now.ToString("MMM")); // Displaying the abbreviated name of the current month
            now = now.AddMonths(1); // Moving to the next month
        }
        Console.WriteLine(); // Outputting a blank line for better readability
    }
}

Sample Output:

Display the name of the first three letters of month of a year :                                             
---------------------------------------------------------------------                                         
 The date of Today : 12/06/2017                                                                               
 The twelve months are :                                                                                      
 Jun                                                                                                      
 Jul                                                                                                      
 Aug                                                                                                      
 Sep                                                                                                      
 Oct                                                                                                      
 Nov                                                                                                      
 Dec                                                                                                      
 Jan                                                                                                      
 Feb                                                                                                      
 Mar                                                                                                      
 Apr                                                                                                      
 May
 

Flowchart:

Flowchart: C# Sharp Exercises - print the name of the first three letters of month of a year starting form current date

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to get the day and month name of current date.
Next: Write a program in C# Sharp to print the name of month in full starting from current date.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.