-->

program to find sum of two distances measured in feet and inch using function.


using codeblocks
---------------------------------------------------------------------------------------
//program to find sum of two distances measured in feet and inch using function.
//passing struct to function
//understanding struct to a function
#include<stdio.h>                           //header file
struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
void sum( struct distance d1,struct distance d2);//function prototype/declaration
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    sum(d1,d2);                               //calls the function
    return 0;
}
void sum(struct distance d1,struct distance d2) //function body line
{
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
}
---------------------------------------------------------------------------------------------------------
or
-----------------------------------------------------------------------------------------
//understanding struct to a function
#include<stdio.h>                           //header file
void sum( d1,d2);//function prototype/declaration with variables
struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    sum(d1,d2);                                            //calls the function
    return 0;
}
void sum(struct distance d1,struct distance d2) //function body line
{
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
}
-------------------------------------------------------------------------------------------------
using turbo c++
------------------------------------------------------------------------------------------------
//understanding struct to a function
#include<stdio.h>                           //header file
#include<conio.h>                       //header file to hold output
struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
void sum( struct distance d1,struct distance d2);//function prototype/declaration
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    sum(d1,d2);                               //calls the function
getch();    
return 0;
}
void sum(struct distance d1,struct distance d2) //function body line
{
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
}

No comments:

Post a Comment