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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework