In this C Program, we have to add two distances given in Feet and Inches. First, we have to store the value of feet and inches in the first distance variable and the second value of feet and inches in the second distance variable after that we will add the first distance feet values to the second distance feet values and same repeat with the first distance inches value with second distance value.
First Test Case
Add two distances using structure in c programming first we need to add feet to feet and inches to inches. So let’s take an example and discuss the first test cases in detail.
The first distance is 6 feet and 4 inches and the second distance is 5 feet and 6 inches then our c program to add two distances in the inch-feet system will give an output of 11 feet and 10 inches. That’s the correct answer. Now discuss the special test case.
Second Test Case(Special One)
Let’s take a special test case for our c program to add two distances given as input in feet and inches. Suppose we have the first distance input is 6 feet and 10 inches, and the second input is 5 feet and 16 inches. Now as I have told you earlier(1 feet’s=12 inches) remember this point this is helping a lot. If we add two distances using structure in c just like the first test case then our output will be 11 feet and 26 inches(6 feet+5 feet=11 feet and 10 inches+16 inches=26 inches).
We have designed a program to add two distances in inch feet system using structure so we are going to convert 26 inches to feet for this we will divide by 12 inches so we got 2 feet and 2 inches of snow we will add these values to the previous feet result.
The final result of the c program is to add two distances given as input in feet and inches we will get 13 feet and 2 inches. Always remember that inches values can’t be greater than 12. Because it would convert into feet. Find the Output at the end.
C Program to Add Two Distances (in Inch-feet) System Using Structures
Below is the program to add two distances in inch feet system using structure. The code has been updated now it’s passed all the test cases.
#include <stdio.h>
struct distance
{
int feets;
int inchs;
};
void addDistance(struct distance firstdistance, struct distance seconddistance)
{
struct distance totalsum;
totalsum.feets = firstdistance.feets + seconddistance.feets;
totalsum.inchs = firstdistance.inchs + seconddistance.inchs;
totalsum.feets = totalsum.feets + totalsum.inchs / 12; //1 feet has 12 inches
totalsum.inchs = totalsum.inchs % 12;
printf("\nTotal Distance is: %d Feets, and %d' Inches", totalsum.feets, totalsum.inchs);
}
int main()
{
/*C Program to Add Two Distance Given as Input in Feet and Inches. distances*/
struct distance firstdistance, seconddistance;
printf("Enter the First Distance in Feets: ");
scanf("%d", &firstdistance.feets);
printf("Enter First Distance in Inches: ");
scanf("%d", &firstdistance.inchs);
printf("\n\nEnter Second Distance in Feets: ");
scanf("%d", &seconddistance.feets);
printf("Enter Second Distance in Inches: ");
scanf("%d", &seconddistance.inchs);
addDistance(firstdistance, seconddistance);
return 0;
}
Program to Add Two Distances in the Inch-Feet System
This programming challenge has some test cases that we have to take care of it, or else it will run for normal test cases, not for all test cases. So don’t worry our program to add two distances in inch feet system using structure is going to pass all the test cases.
First Test Case
Add two distances using structure in c programming first we need to add feet to feet and inches to inches. So let’s take an example and discuss the first test cases in detail.
The first distance is 6 feet and 4 inches and the second distance is 5 feet and 6 inches then our c program to add two distances in the inch-feet system will give an output of 11 feet and 10 inches. That’s the correct answer. Now discuss the special test case.
Second Test Case(Special One)
Let’s take a special test case for our c program to add two distances given as input in feet and inches. Suppose we have the first distance input is 6 feet and 10 inches, and the second input is 5 feet and 16 inches. Now as I have told you earlier(1 feet’s=12 inches) remember this point this is helping a lot. If we add two distances using structure in c just like the first test case then our output will be 11 feet and 26 inches(6 feet+5 feet=11 feet and 10 inches+16 inches=26 inches).
We have designed a program to add two distances in inch feet system using structure so we are going to convert 26 inches to feet for this we will divide by 12 inches so we got 2 feet and 2 inches of snow we will add these values to the previous feet result.
The final result of the c program is to add two distances given as input in feet and inches we will get 13 feet and 2 inches. Always remember that inches values can’t be greater than 12. Because it would convert into feet. Find the Output at the end.
0 Comments: