Friday, 15 April 2016

C Program To Add Two Distances ( in inch-feet ) Using Structures

Problem :- C Program To Add Two Distances ( in inch-feet ) System Using Structures In C .

What Is Structure 

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 more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store collection of any type of data.


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


Defining a structure


struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.


Syntax :


struct structure_name

{
 //Statements
};

Solution :-

#include <stdio.h>

struct Distance{
    int feet;
    float inch;
}d1,d2,sum;
int main()
{
//Ghanendra Yadav
    printf("Enter information for 1st distance\n");
    printf("Enter feet: ");
    scanf("%d",&d1.feet);
    printf("Enter inch: ");
    scanf("%f",&d1.inch);
    printf("\nEnter information for 2nd distance\n");
    printf("Enter feet: ");
    scanf("%d",&d2.feet);
    printf("Enter inch: ");
    scanf("%f",&d2.inch);
    sum.feet=d1.feet+d2.feet;
    sum.inch=d1.inch+d2.inch;

    if (sum.inch>12.0)

    {
        sum.inch=sum.inch-12.0;
        ++sum.feet;
    }
    printf("\nSum of distances=%d\'-%.1f\"",sum.feet,sum.inch);
    return 0;
}

Output:-


c program to add two distances in inch feet system using structures

No comments:
Write comments

Recommended Posts × +