4/15/2016

C Program to Add Two Distance Given as Input in Feet and Inches

Write a solution for c program to add two distance given as input in feet and inches. This is a programming challenge we got now we have to write a program to add two distances in the inch-feet system. We are going to explain some test cases down below.

This programming challenge has some test cases that we have to take care of it, 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.

C Program to Add Two Distances in Inch-Feet System


In this C program, we have to add two distances are given in Feet and Inches. First, we have to store the value of feet and inches in first distance variable and the second value feet and inches in second distance variable after that we will add first distance feets values to second distance feets values and same repeat with the first distance inches value with second distance value.

Let’s discuss this programming challenge(I.e. Hackerrank Programming Challenge) we have in details and find out the solution with proper explanations and our program code passed all the test cases.

Program to Add Two Distances in Inch Feet System Using Structure


There are two special test cases to this programming problem, we are going to talk about both in this solution one by one. Please keep in mind that 1 Foot is equal to 12 inches(1 feet’s=12 inch). Because this is going to help us a lot.

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 details.

Write a C Program to Add Two Distances in Inch Feet System Using Structure

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 11 feet and 10 inches. That’s a 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 distance given as input in feet and inches. Suppose we have 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 inch) remember this point this is helping a lot. If we add two distances using structure in c just like a 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 snow we will add this values to previous feet result. We are also using Format Specifiers in this program

The final result of c program to add two distance 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 the 12. Because it would convert into feet. Find the Output in the end.

What Is Structure


The structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in a more meaningful way. The structure is going to help a lot to add two distances using structure in c.

A structure is used to represent a record. Suppose you want to store a record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.

C Program to Add Two Distance Given as Input in Feet and Inches Solution


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;
}

The Output of Program to Add Two Distances in Inch-Feet System


write a c program to add two distance given as input in feet and inches

Similar to Add Two Distances Using Structure in C

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: