Program to Convert Given Inches into Equivalent Yards and Feet

Posted in

Program to Convert Given Inches into Equivalent Yards and Feet
vinaykhatri

Vinay Khatri
Last updated on October 6, 2024

    A unit is a known quantity used in measurement. Unit conversion is the process of converting one unit into another. When calculating the distance, inches, feet, and yards are popular units. If you are a beginner in computer programming , writing a program to convert units is entry-level.

    In this tutorial, we will write a program that can accept length in inches and convert it to the corresponding yard and feet. It is extremely simple to write this program. You only need to know the syntax of the programming language in which you want to write and the formula for converting inches into yards and feet.

    Formula to Convert Inches into Yards and Feet

    If we look at the length converter, there are 36 inches in 1 yard and 12 inches in 1 foot.

    So, the formula to convert inches into yards is

    Yard = Inches / 36

    The formula to convert inches into feet is

    Foot = Inches / 12

    Now, let's write our program to convert the given inches into equivalent yards and feet in different programming languages - C, C++, Java, and Python.

    C Program to Convert Given Inches into Equivalent Yards and Feet

    #include <stdio.h>
    
    int main() 
    {
        float inch, foot, yard;
        
        //ask user to enter Inches
    	printf("Enter Inches: ");
        scanf("%f", &inch);
        
        //convert inches into yards
        yard = inch/36;
        //convert inches into feet
        foot = inch/12;
        
        printf("There are %.2f yard(s) in %.2f inch(es)\n", yard, inch);
        printf("There are %.2f foot in %.2f inch(es)\n", foot, inch);
        
        return 0;
    }

    Output

    Enter Inches: 12
    There are 0.33 yard(s) in 12.00 inch(es)
    There are 1.00 foot in 12.00 inch(es)

    C++ Program to Convert Given Inches into Equivalent Yards and Feet

    #include <iostream>
    using namespace std;
    
    int main() 
    {
       float inch, foot, yard;
        
        //ask user to enter Inches
        cout<<"Enter Inches: "; 
        cin>>inch;
        
        //convert inches into yards
        yard = inch/36;
        //convert inches into feet
        foot = inch/12;
        
        cout<<"There are "<<yard<<" yard(s) in "<<inch<<" inch(es)\n";
        cout<<"There are "<<foot<<" foot in " <<inch<<" inch(es)\n";
        
        return 0;
    }

    Output

    Enter Inches: 13
    There are 0.361111 yard(s) in 13 inch(es)
    There are 1.08333 foot in 13 inch(es)

    Python Program to Convert Given Inches into Equivalent Yards and Feet

    inch = int(input("Enter Inches: "))
    
    # convert inches into yards
    yard = inch/36
    # convert inches into feet
    foot = inch/12
    
    print(f"There are {round(yard,4)} yard(s) in {inch} inch(es)")
    print(f"There are {round(foot,4)} foot in {inch} inch(es)")

    Output

    Enter Inches: 23
    There are 0.6389 yard(s) in 23 inch(es)
    There are 1.9167 foot in 23 inch(es)

    Java Program to Convert Given Inches into Equivalent Yards and Feet

    import java.util.*;
    class Main {
        public static void main(String[] args) {
            float inch, foot, yard;
            Scanner ss = new Scanner(System.in);
            System.out.println("Please enter inches:");
            inch = ss.nextFloat();
            yard = inch / 36;
            System.out.println("There are " + yard +" yard(s) " + inch +" in inches ");
            foot = inch /12;
            System.out.println("There are " + foot +" feet " + inch +" in inches ");
        }
    }
    
    

    Output

    Please enter inches:
    35
    There are 0.9722222 yard(s) 35.0 in inches 
    There are 2.9166667 feet 35.0 in inches 

    Complexity Analysis

    • Time Complexity : O(1), constant time complexity.
    • Space Complexity: O(1), constant space complexity

    Conclusion

    These were C, C++, Python, and Java programs to convert user-entered inches into yards and feet. The program is quite straightforward and works on simple arithmetic operations. Only the programming language syntax and the formulas to convert inches into yards and feet are needed.

    If you like this article or have any suggestions, please let us know by commenting below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments