Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

Add two lengths

Get two lengths in feet and inches and add them

Sample Input 1:

6:03 4:11

Sample Output 1:

11:02

Program or Solution

					    
							
#include<stdio.h>
struct length{
	int feet;
	int inch;
};

int main()
{
	struct length l1,l2,l3;
	printf("Enter First Length:");
	scanf("%d:%d",&l1.feet,&l1.inch);
	printf("Enter second Length:");
	scanf("%d:%d",&l2.feet,&l2.inch);
	l3.inch=l1.inch+l2.inch;
	l3.feet=l1.feet+l2.feet;
	if(l3.inch>11)
	{
		l3.feet++;
		l3.inch=l3.inch-12;
	}
	printf("%d:%d",l3.feet,l3.inch);
	return 0;
}
					    
					

Program Explanation

l1,l2 and l3 are structure variables, each occupies 8 btyes.

4 bytes for feet and 4 bytes for feet.

Add inches of l1 and l2 and store it in l3, then add feets of l1 and l2 and store it in l3.

if l3 inches is greater than 11 then increment l3 feet by and decrement l3 inch by 12.