-->

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

using codeblocks
------------------------------------------------------------
//using typedef and function to print sum of two feet and inches
#include <stdio.h>
 typedef struct               //creating a new tag called feet_inch and supplying struct property to it.
{
    int feet1,feet2;          //members
    int inch1,inch2;
    int total_feet,total_inch;
}feet_inch;                  //works like tag; but is a variable
void sum( feet_inch var, feet_inch var1);   //function prototype.
int main()
{
    feet_inch var, var1;       // creating two variables
    printf("enter first feet and inch\n");
    scanf("%d%d",&var.feet1,&var.inch1);//gets input
    printf("enter second feet and inch\n");
    scanf("%d%d",&var1.feet2,&var1.inch2);//gets input
    sum(var,var1);                         //calling with parameter var and var1
    return 0;
}
void sum( feet_inch var,feet_inch var1)
{
 feet_inch total;                          // creating third variable to store total
total.total_feet=var.feet1+var1.feet2+(var.inch1+var1.inch2)/12;//finds total feet
total.total_inch=(var.inch1+var1.inch2)%12;                //finds total inch
printf("total feet=%d and total inch=%d\n",total.total_feet,total.total_inch);//prints output
}

//note: Let's not declare function at the top like we do in functions. If it is then it would not work.
-----------------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//using typedef and function to print sum of two feet and inches
#include <stdio.h>
#include<conio.h>
 typedef struct               //creating a new tag called feet_inch and supplying struct property to it.
{
    int feet1,feet2;          //members
    int inch1,inch2;
    int total_feet,total_inch;
}feet_inch;                  //works like tag; but is a variable
void sum( feet_inch var, feet_inch var1);   //function prototype.
int main()
{
    feet_inch var, var1;       // creating two variables
    printf("enter first feet and inch\n");
    scanf("%d%d",&var.feet1,&var.inch1);//gets input
    printf("enter second feet and inch\n");
    scanf("%d%d",&var1.feet2,&var1.inch2);//gets input
    sum(var,var1);                         //calling with parameter var and var1
   getch();
    return 0;
}
void sum( feet_inch var,feet_inch var1)
{
 feet_inch total;                          // creating third variable to store total
total.total_feet=var.feet1+var1.feet2+(var.inch1+var1.inch2)/12;//finds total feet
total.total_inch=(var.inch1+var1.inch2)%12;                //finds total inch
printf("total feet=%d and total inch=%d\n",total.total_feet,total.total_inch);//prints output
}

//note: Let's not declare function at the top like we do in functions. If it is then it would not work.


No comments:

Post a Comment