Nested structs C# Exercise - C# Programming Course

 Exercise

Nested structs

 Objetive

Write a C# Struct to store two data for a person:

name and date of birth.

The date of birth must be another struct consisting on day, month and year.

Finally, create an array of persons, ask the user for the data of two persons and display them.

 Example Code

using System;  // Importing the System namespace to access basic classes like Console

class Program
{
    // Define a struct for Date of Birth with three fields: Day, Month, and Year
    struct DateOfBirth
    {
        public int Day;    // Day of birth
        public int Month;  // Month of birth
        public int Year;   // Year of birth
    }

    // Define a struct for Person that includes a Name and a DateOfBirth
    struct Person
    {
        public string Name;          // Name of the person
        public DateOfBirth BirthDate; // Date of birth of the person
    }

    static void Main()
    {
        // Create an array of Persons to store the data for two people
        Person[] persons = new Person[2];

        // Ask the user to input data for two persons
        for (int i = 0; i < persons.Length; i++)
        {
            // Ask the user to enter the name of the person
            Console.Write($"Enter the name of person {i + 1}: ");
            persons[i].Name = Console.ReadLine();  // Store the name in the Name field of the struct

            // Ask the user to enter the birth day of the person
            Console.Write($"Enter the birth day of person {i + 1}: ");
            persons[i].BirthDate.Day = int.Parse(Console.ReadLine()); // Store the birth day in the Day field of the nested struct

            // Ask the user to enter the birth month of the person
            Console.Write($"Enter the birth month of person {i + 1}: ");
            persons[i].BirthDate.Month = int.Parse(Console.ReadLine()); // Store the birth month in the Month field of the nested struct

            // Ask the user to enter the birth year of the person
            Console.Write($"Enter the birth year of person {i + 1}: ");
            persons[i].BirthDate.Year = int.Parse(Console.ReadLine()); // Store the birth year in the Year field of the nested struct
        }

        // Display the data of both persons
        for (int i = 0; i < persons.Length; i++)
        {
            Console.WriteLine($"Person {i + 1}:");  // Print the person index (1 or 2)
            Console.WriteLine($"Name: {persons[i].Name}");  // Display the name of the person
            Console.WriteLine($"Birth Date: {persons[i].BirthDate.Day}/{persons[i].BirthDate.Month}/{persons[i].BirthDate.Year}");  // Display the birth date of the person
        }
    }
}

More C# Exercises of Arrays, Structures and Strings

 Reverse array
Write a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he ...
 Array of even numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
 Many numbers and sum
Write a C# program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must...
 Two dimensional array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
 Statistics V2
Write a C# statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been en...
 Struct
Write a C# Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
 Array of struct
Write a C# program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the use...
 Array of struct and menu
Write a C# program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for...
 Books database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
 Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
 Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
 Centered triangle
Write a C# program that Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Write a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Write a C# program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Displ...
 Sort data
Write a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Write a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays the...
 Two dimensional array 2: circunference on screen
Write a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it o...
 Computer programs
Write a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category ...
 Exercise tasks
Write a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month and...
 Household accounts
Write a C# program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), sh...

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.