Legal Age Checker in CSharp

Legal Age Checker in CSharp

Introduction

Welcome to our C# programming lesson! In this tutorial, we’ll explore the basics of conditional statements in C# by creating a straightforward legal age checker. This program will prompt users to input their age, and based on their input, it will determine whether they are considered a minor or an adult.

Understanding control flow in programming is crucial, and the if-else statement is a fundamental building block for implementing decision-making logic in your code. By the end of this lesson, you’ll have a functional C# console program that demonstrates the usage of if-else statements to make decisions based on user input.

Prerequisites

Before we dive into the lesson, make sure you have the following:

  • Basic knowledge of C# syntax.
  • A C# development environment set up (Visual Studio, Visual Studio Code, or any C# compiler).

The Legal Age Checker Program

Our program will follow a simple structure:

  1. User Input: Prompt the user to enter their age.
  2. Age Verification: Use an if-else statement to determine if the entered age is less than 18 (considered a minor) or 18 and above (considered an adult).
  3. Output: Display a message indicating whether the user is a minor or an adult.

Let’s dive into the code and explore each step. Feel free to follow along, and don’t hesitate to experiment with the code to solidify your understanding.

Objectives

In this lesson, our overarching objective is to introduce fundamental programming concepts through the lens of creating a legal age checker in C# using the if-else statement. We aim to provide learners with a solid understanding of conditional statements, emphasizing their role in decision-making within programs. By guiding learners through the process of user input handling, numeric comparison, and variable usage, the lesson aims to instill the foundational skills needed to build interactive and responsive C# applications. Throughout the tutorial, we underscore the significance of error handling, user feedback, and adherence to coding best practices to cultivate robust and readable code. The culmination of the lesson involves the practical application of these concepts in the development of a functional legal age checker, empowering learners to apply their newfound knowledge in real-world programming scenarios.

  1. Understand the Basics of Conditional Statements:
    • Learn the fundamental concepts of conditional statements in C#.
    • Explore how if-else statements are used to implement decision-making logic in programs.
  2. User Input Handling:
    • Gain experience in prompting users for input in a C# console application.
    • Learn how to read and parse user input for further processing.
  3. Conditional Logic with if-else:
    • Explore the syntax and structure of the if-else statement in C#.
    • Understand how to express conditional checks based on user input.
  4. Variable Declaration and Assignment:
    • Practice declaring and initializing variables to store user input and perform calculations.
  5. Numeric Comparison:
    • Use numeric comparison operators to check if the user’s age meets specific conditions.
  6. Output Generation:
    • Learn how to generate meaningful output messages based on the results of conditional checks.
    • Explore methods for displaying information to the user in a console application.
  7. Error Handling and User Feedback:
    • Implement basic error handling to address scenarios where user input is not a valid number.
    • Provide clear and helpful feedback to users in case of input errors.
  8. Application Flow Control:
    • Understand how the flow of the program is controlled by conditional statements.
    • Grasp the concept of code execution paths based on different conditions.
  9. Code Readability and Best Practices:
    • Discuss coding conventions and best practices for writing clear and readable code.
    • Explore ways to enhance the maintainability of the program.
  10. Practical Application:
    • Apply the knowledge gained throughout the lesson to create a practical C# console program.
    • Build a legal age checker that users can interact with and receive personalized feedback.

By the end of this lesson, you should have a solid understanding of conditional statements in C# and be able to create a functional legal age checker program. These objectives aim to provide a hands-on and practical approach to learning fundamental programming concepts in C#.

25 Best C# Project Ideas
25 Best C# Project Ideas

Source code

using System;

namespace LegalAgeChecker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter your age:");

// Read user input as a string
string ageInput = Console.ReadLine();

// Try to parse the input as an integer
if (int.TryParse(ageInput, out int age))
{
// Check if the age is less than 18
if (age < 18)
{
Console.WriteLine("You are a minor.");
}
else
{
Console.WriteLine("You are an adult.");
}
}
else
{
Console.WriteLine("Invalid input. Please enter a valid age as a number.");
}
Console.WriteLine("Press any key to close the console.");
Console.ReadKey();
}
}
}

Explanation

  1. The program prompts the user to enter their age.
  2. It reads the user input as a string.
  3. It attempts to parse the input as an integer using int.TryParse.
  4. If parsing is successful, it checks whether the age is less than 18 and prints the corresponding message.
  5. If parsing fails, or if the age is not a valid integer, it notifies the user about the invalid input.

This program ensures that the user inputs a valid age as a number and provides an appropriate response based on whether the person is a minor or an adult.

FREE DOWNLOAD SOURCE CODE

Conclusion

In conclusion, this lesson has provided a foundational understanding of C# programming through the creation of a practical legal age checker. We explored the core concept of conditional statements, specifically the if-else statement, and its role in decision-making within programs. By navigating through user input handling, numeric comparison, variable usage, and application flow control, learners gained insights into building interactive and responsive C# applications. The emphasis on error handling, user feedback, and adherence to coding best practices contributes to the development of robust and readable code. Through the hands-on experience of creating a functional legal age checker, learners are not only equipped with fundamental programming skills but also primed for further exploration into advanced C# concepts and problem-solving challenges. As you embark on the next steps of your coding journey, the knowledge gained in this lesson serves as a solid foundation for building more sophisticated applications and honing your programming expertise. Congratulations on completing this lesson, and happy coding!

Exercises and Activities

Exercises:

  1. Extended Age Ranges:
    • Modify the program to include different age ranges such as “Child,” “Teenager,” and “Senior” in addition to “Minor” and “Adult.”
  2. Multiple Conditions:
    • Extend the program to include multiple conditions, such as checking if the user is a teenager before checking if they are a minor or adult.
  3. Custom Messages:
    • Allow users to input custom messages when their age falls into a specific category. For example, adults can input a message about their responsibilities.
  4. Error Handling Enhancement:
    • Improve the error handling mechanism to provide specific feedback when the user enters non-numeric input or a negative age.

Assessment Tasks:

  1. Code Review:
    • Conduct a code review of the legal age checker program. Assess the clarity of variable names, the use of comments, and adherence to coding conventions.
  2. Scenarios and Testing:
    • Create different scenarios for testing the program, including edge cases. Evaluate the program’s accuracy in categorizing users based on age.
  3. Input Validation:
    • Test the program with various forms of input, including invalid age inputs. Ensure the program handles such inputs gracefully and provides appropriate feedback.
  4. Refactoring Challenge:
    • Challenge learners to refactor the program by creating a separate method for age classification. Evaluate the impact on code readability and maintenance.
  5. Complex Conditions:
    • Introduce more complex conditions, such as considering both age and a boolean variable indicating whether the person has a legal guardian.
  6. Enhanced User Interaction:
    • Modify the program to offer additional interactions, such as asking the user if they would like to input another age after the initial classification.
  7. Unit Testing:
    • Introduce unit tests to verify the correctness of the age classification function. Evaluate the effectiveness of the tests in capturing different scenarios.
  8. Performance Optimization:
    • Challenge learners to optimize the program’s performance, considering aspects like memory usage and execution speed.
  9. Localization Support:
    • Extend the program to support multiple languages for user prompts and messages.
  10. Integration with External Data:
    • Explore integrating the program with external data sources, such as an age classification service or an age-related dataset.

These exercises and assessment tasks aim to deepen the understanding of the legal age checker program and provide opportunities for learners to apply advanced programming concepts. They cover a range of aspects, including code quality, input validation, user interaction, and advanced condition handling.

Related Topics and Articles:

C# IF ELSE IF Statement Video Tutorial and Source code

C# IF ELSE Statement Video Tutorial and Source code

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation