Skip to content

Add AgeCalculator sample app #7053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/console-apps/AgeCalculator/AgeCalculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
35 changes: 35 additions & 0 deletions core/console-apps/AgeCalculator/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
20 changes: 20 additions & 0 deletions core/console-apps/AgeCalculator/README.md
Original file line number Diff line number Diff line change
@@ -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.