diff --git a/core/console-apps/AgeCalculator/AgeCalculator.csproj b/core/console-apps/AgeCalculator/AgeCalculator.csproj new file mode 100644 index 00000000000..21114b11099 --- /dev/null +++ b/core/console-apps/AgeCalculator/AgeCalculator.csproj @@ -0,0 +1,8 @@ + + + + Exe + net8.0 + + + diff --git a/core/console-apps/AgeCalculator/Program.cs b/core/console-apps/AgeCalculator/Program.cs new file mode 100644 index 00000000000..40c96f77d2c --- /dev/null +++ b/core/console-apps/AgeCalculator/Program.cs @@ -0,0 +1,35 @@ +using System; + +namespace AgeCalculator +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("=== Age Calculator ==="); + + Console.Write("Enter your date of birth (yyyy-mm-dd): "); + string input = Console.ReadLine(); + + if (DateTime.TryParse(input, out DateTime dob)) + { + DateTime today = DateTime.Today; + int age = today.Year - dob.Year; + if (dob > today.AddYears(-age)) age--; + + Console.WriteLine($"You are {age} years old."); + + DateTime nextBirthday = dob.AddYears(age + 1); + int daysUntil = (nextBirthday - today).Days; + Console.WriteLine($"Your next birthday is in {daysUntil} days."); + } + else + { + Console.WriteLine("Invalid date format. Please use yyyy-mm-dd."); + } + + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(); + } + } +} diff --git a/core/console-apps/AgeCalculator/README.md b/core/console-apps/AgeCalculator/README.md new file mode 100644 index 00000000000..008fff2b08c --- /dev/null +++ b/core/console-apps/AgeCalculator/README.md @@ -0,0 +1,20 @@ +# Age Calculator Sample + +This is a beginner-friendly .NET 8 console app that calculates a user's age and days until their next birthday based on their date of birth input. + +## Key Features + +- Reads user input for date of birth +- Calculates current age +- Displays days until next birthday +- Includes basic error handling for invalid input + +## Build and Run + +To build and run the sample, type the following command: + +`dotnet run` + +`dotnet run` builds the sample and runs the output assembly. It implicitly calls `dotnet restore` on .NET Core 2.0 and later versions. If you're using a .NET Core 1.x SDK, you first have to call `dotnet restore` yourself. + +**Note:** Starting with .NET Core 2.0 SDK, you don't have to run [`dotnet restore`](https://docs.microsoft.com/dotnet/core/tools/dotnet-restore) because it's run implicitly by all commands that require a restore to occur, such as `dotnet new`, `dotnet build` and `dotnet run`. It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as [continuous integration builds in Azure DevOps Services](https://docs.microsoft.com/azure/devops/build-release/apps/aspnet/build-aspnet-core) or in build systems that need to explicitly control the time at which the restore occurs.