assignment-s-22.c ​
Metadata ​
- Author — Amit Dutta (amitdutta4255@gmail.com)
- Last updated — 18 Jan 2026
- License — MIT
Problem Statement ​
Problem Statement
Write a program using structures to add two distances in meter-kilometer format.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <stdlib.h>
struct distance
{
int km;
int m;
};
void total_distance(struct distance[]);
int main()
{
struct distance dis[2] = {0};
printf("Enter the 1st distance (KM M): ");
scanf("%d %d", &dis[0].km, &dis[0].m);
printf("Enter the 2nd distance: ");
scanf("%d %d", &dis[1].km, &dis[1].m);
total_distance(dis);
return 0;
}
void total_distance(struct distance dis[])
{
int result_km = dis[0].km + dis[1].km;
int result_m = dis[0].m + dis[1].m;
result_km += result_m / 1000;
result_m = result_m % 1000;
printf("Total distance: %d KM, %d M", result_km, result_m);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30