Program to Add Two Distances (in inch-feet system) using Structures - (C# Basic Programs)
In this tutorial, we will discuss a program to add two distances (in inch-feet system) using structures in C# programming language. This program will help demonstrate the use of structures and their ability to group related data types into a single unit.
Syntax
The syntax for creating a structure in C# is as follows:
accessSpecifier struct structureName {
dataType variableName1;
dataType variableName2;
...
dataType variableNameN;
}
Example
using System;
namespace DistanceAddition {
struct Distance {
public int feet;
public int inch;
public void setData() {
Console.Write("Enter feet: ");
feet = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter inches: ");
inch = Convert.ToInt32(Console.ReadLine());
}
public void printData() {
Console.WriteLine("Feet: " + feet + " Inches: " + inch);
}
}
class Program {
static void Main(string[] args) {
Distance d1 = new Distance();
Distance d2 = new Distance();
Distance sum = new Distance();
Console.WriteLine("Enter information for the first distance: ");
d1.setData();
Console.WriteLine("Enter information for the second distance: ");
d2.setData();
sum.feet = d1.feet + d2.feet;
sum.inch = d1.inch + d2.inch;
if (sum.inch >= 12) {
sum.feet += sum.inch / 12;
sum.inch = sum.inch % 12;
}
Console.WriteLine("\nTotal Distance: ");
sum.printData();
Console.Read();
}
}
}
Output:
Enter information for the first distance:
Enter feet: 5
Enter inches: 2
Enter information for the second distance:
Enter feet: 3
Enter inches: 5
Total Distance:
Feet: 8 Inches: 7
Explanation
In this program, we have defined a structure named Distance
. This structure has two data fields: feet
and inch
. We have also defined two methods: setData()
and printData()
, to set and display the values of the data fields.
In the Main()
method, we have created three instances of the Distance
structure: d1
, d2
, and sum
. We have then used the setData()
method to set the values of d1
and d2
. We have added the values of d1
and d2
and stored the result in the sum
structure.
We have also added a condition to the program that checks whether the sum of the inches is greater than or equal to 12. If it is, we increment the sum of the feet by the number of times 12 fits into the sum of the inches and set the sum of the inches to the remainder.
Finally, we have used the printData()
method to display the value of the sum
structure.
Use
This program demonstrates the use of structures in C# and illustrates how they can be used to group related data types into a single unit. It also provides a practical application of the structure data type by allowing the user to add two distances represented in the inch-feet system.
Summary
In this tutorial, we have discussed a program to add two distances (in the inch-feet system) using structures in C#. We have seen the syntax, example, explanation, use, and summary of the program. This program is a practical application of the structure data type and demonstrates how it can be used to group related data types together into a single unit.