Age - C# Programming Exercise

This C# exercise is perfect for learning how to interact with the user and use input data to personalize the program's output. In this case, the program will ask the user to enter their age (for example, 20), and then display a message like "You look younger than 20," where the number 20 will be replaced by the age the user entered.

The purpose of this exercise is to work with Console.ReadLine() to capture user input, and then use that input to generate a dynamic response on the screen using Console.WriteLine(). This type of interaction is crucial for creating applications that respond to user actions.

Additionally, you will learn how to use variables and display personalized messages in C#, which will help you improve your skills in creating more interactive and dynamic programs.

 Category

First contact with C# Sharp

 Exercise

Age

 Objective

Write a C# program to ask the user for their age (e.g. 20) and respond with something like "You look younger than 20" (the age entered by the user should be displayed in place of "20").

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

// Main class of the program
class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Declaring a variable to store the user's age
        int age;

        // Asking the user to enter their age and reading the input
        Console.Write("Please enter your age: ");
        age = Convert.ToInt32(Console.ReadLine());

        // Printing the response with the age entered by the user
        Console.WriteLine("You look younger than {0}", age);
    }
}

 Share this C# exercise

 More C# Programming Exercises of First contact with C# Sharp

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Formats

    This C# exercise teaches you how to handle console output using two different methods: Console.Write and formatting with {0}. In this program, the user will in...

  •  Rectangle

    This C# exercise helps you practice using loops and user input. In this program, the user will be asked to input a number (a digit), and the program will display a re...

  •  Conversion

    This C# exercise teaches you how to perform temperature unit conversions. The program prompts the user to input a temperature in Celsius degrees and then converts it ...

  •  First contact with C#

    This exercise is an excellent way to get started with programming in C#. In this program, you will learn how to print a message on the screen using C#. The program wi...

  •  Sum of two numbers

    This exercise is a great way to practice basic arithmetic operations in C#. In this program, you will learn how to perform an addition operation between two numbers, 12 and ...

  •  Division of two numbers

    This exercise is a great opportunity to practice the division operation in C#. In this program, you will learn how to divide two numbers, in this case, 24 and 5, and ...