In this program, we will learn how to add two distances in C programming using structure.

## C Program to add two user input distances using structure

The explanation of the program is provided at the end of the following code. The brief description of important statements and function is provided in the program itself using comments.

#include <stdio.h>

// Structure to store distance in feet and inches

struct Distance {

int feet;

float inches;

};

// Function to add two distances

struct Distance addDistances(struct Distance d1, struct Distance d2) {

struct Distance result;

// Add feet and inches separately

result.feet = d1.feet + d2.feet;

result.inches = d1.inches + d2.inches;

// If inches is greater than or equal to 12, convert to feet

if (result.inches >= 12.0) {

result.feet += (int)(result.inches / 12);

result.inches = fmod(result.inches, 12.0);

}

return result;

}

int main() {

struct Distance d1, d2, result;

// Input first distance

printf("Enter feet and inches for the first distance: ");

scanf("%d %f", &d1.feet, &d1.inches);

// Input second distance

printf("Enter feet and inches for the second distance: ");

scanf("%d %f", &d2.feet, &d2.inches);

// Add the distances

result = addDistances(d1, d2);

// Output the result

printf("Sum of distances: %d feet %.2f inches\n", result.feet, result.inches);

return 0;

}

**Output:** Assume user enters the first distance as 4 7.5 where 4 is feet and 7.5 is inches. The second distance entered by user is 6 6.5.

Enter feet and inches for the first distance: 4 7.5

Enter feet and inches for the second distance: 6 6.5

Sum of distances: 11 feet 2.00 inches

## How this program works?

### Step 1: Define the Structure

We define a structure `Distance`

to store distance in feet and inches.

#include <stdio.h>

#include <math.h> // For fmod function

struct Distance {

int feet;

float inches;

};

### Step 2: Function to Add Two Distances

In order to add two distance we have the function `addDistances()`

that takes two `Distance`

structures, adds them, and returns the result.

`result.feet = d1.feet + d2.feet;`

and`result.inches = d1.inches + d2.inches;`

add the respective feet and inches.`if (result.inches >= 12.0) { ... }`

checks if inches are 12 or more.`result.feet += (int)(result.inches / 12);`

converts excess inches to feet.`result.inches = fmod(result.inches, 12.0);`

uses the`fmod`

function to keep only the fractional part less than 12. (Note: The`fmod`

function in C is used to compute the remainder of the division of two floating-point numbers. It is part of the`<math.h>`

library)

### Step 3: Main Function to Get Input and Display Output

In the `main`

function, we take input for two distances, call the `addDistances()`

function, and print the result.

`struct Distance d1, d2, result;`

declares three`Distance`

variables.`printf`

and`scanf`

are used to take input from user and store the entered values in structures`d1`

and`d2`

.`result = addDistances(d1, d2);`

calls the function to add distances.`printf("Sum of distances: %d feet %.2f inches\n", result.feet, result.inches);`

prints the result.

## Leave a Reply