Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Create a nested structure and store data in an array

C# Sharp STRUCTURE: Exercise-3 with Solution

Write a program in C# Sharp to create a nested structure to store two data for an employee in an array.

Sample Solution:-

C# Sharp Code:

using System;
    class strucExer3
    {
	//employee is a structure of members eName and dtObirth
        struct employee
        {
            public string eName;
            public dtObirth Date;
        }
	//dtObirth is a nested structure of employee
        struct dtObirth
        {
            public int Day;
            public int Month;
            public int Year;
        }  
        static void Main(string[] args)
        {
             
            int dd=0, mm=0, yy=0;
            int total = 2;
			Console.Write("\n\nCreate a nested structure and store data in an array :\n");
			Console.Write("-------------------------------------------------------\n"); 	
            employee[] emp = new employee[total];
 
            for (int i = 0; i < total; i++)
            {
                Console.Write("Name of the employee : ");
                string nm = Console.ReadLine();
                emp[i].eName = nm;
 
                Console.Write("Input day of the birth : ");
                dd = Convert.ToInt32(Console.ReadLine());
                emp[i].Date.Day = dd;
 
                Console.Write("Input month of the birth : ");
                mm = Convert.ToInt32(Console.ReadLine());
                emp[i].Date.Month = mm;
 
                Console.Write("Input year for the birth : ");
                yy = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine();
                emp[i].Date.Year = yy;
            }
        }
    }

Sample Output:

Create a nested structure and store data in an array :                                                           
-------------------------------------------------------                                                       
Name of the employee : H.Rana                                                                                 
Input day of the birth : 04                                                                                   
Input month of the birth : 05                                                                                 
Input year for the birth : 1787

                                                                                
Name of the employee : D.Das                                                                                  
Input day of the birth : 08                                                                                   
Input month of the birth : 04                                                                                 
Input year for the birth : 1784  

Flowchart:

Flowchart: Create a nested structure and store data in an array.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to declare a simple structure and use of static fields inside a struct.
Next: Write a program in C# Sharp to create a structure and assign the value and call it.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.