w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Compute what day will be tomorrow

Secondary Nav

C# Sharp DateTime : Exercise-46 with Solution

Write a program in C# Sharp to get the first day of the current year and first of a year against a given date.

Test Data:

Input the Day : 12
Input the Month : 12
Input the Year : 2012

Expected Output :

 The formatted Date is : 12/12/2012
 The First day of the year 2012 is : 01/01/2012

C# Sharp Code:

using System;

class dttimeex46
{
    static void Main()
    {
	int dt,mn,yr;
    Console.Write("\n\n Find  the first day of a year against a date :\n");
	Console.Write("---------------------------------------------------\n");	
	Console.WriteLine(" The First day of the current year is : {0} \n", FirstDayOfYear().ToString("dd/MM/yyyy"));
    Console.Write(" Input the Day : ");
    dt = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Month : ");
    mn = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Year : ");
    yr = Convert.ToInt32(Console.ReadLine());		
	DateTime d = new DateTime(yr, mn, dt);
	Console.WriteLine(" The formatted Date is : {0}",d.ToString("dd/MM/yyyy"));
    DateTime nd=FirstDayOfYear(d);
	Console.WriteLine(" The First day of the year {0} is : {1}\n", yr,nd.ToString("dd/MM/yyyy"));
    }
    static DateTime FirstDayOfYear()
    {
	return FirstDayOfYear(DateTime.Today);
    }
    static DateTime FirstDayOfYear(DateTime y)
    {
	return new DateTime(y.Year, 1, 1);
    }
}

C# Sharp Practice online:

using System;
class dttimeex46
{
    static void Main()
    {
	int dt,mn,yr;
    Console.Write("\n\n Find  the first day of a year against a date :\n");
	Console.Write("---------------------------------------------------\n");	
	Console.WriteLine(" The First day of the current year is : {0} \n", FirstDayOfYear().ToString("dd/MM/yyyy"));
    Console.Write(" Input the Day : ");
    dt = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Month : ");
    mn = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Year : ");
    yr = Convert.ToInt32(Console.ReadLine());		
	DateTime d = new DateTime(yr, mn, dt);
	Console.WriteLine(" The formatted Date is : {0}",d.ToString("dd/MM/yyyy"));
    DateTime nd=FirstDayOfYear(d);
	Console.WriteLine(" The First day of the year {0} is : {1}\n", yr,nd.ToString("dd/MM/yyyy"));
    }
    static DateTime FirstDayOfYear()
    {
	return FirstDayOfYear(DateTime.Today);
    }
    static DateTime FirstDayOfYear(DateTime y)
    {
	return new DateTime(y.Year, 1, 1);
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program