Problem
Create a variable kilos to store the weight in kilograms and convert it into pounds using Matlab. Show the result in a neat text fashion. The conversion factor is 1 Pound = 0.453592 Kilograms.
Solution
This problem has been a sequel of the recent post on radiusofcircle.blogspot.com and can be found here: Weight to convert Pounds to Kilograms. This post is similar to that and if you have solved the prequel post then I suggest you to solve this problem by your own and verify the solution with the one posted here. In this problem we will request the user to input the value of the weight in kilograms and store it in a variable. Now we will use the conversion factor and store the value of the result in another variable. We will finally display the value to the user.Some of the key concepts in that you need to know in Matlab to solve this problem are as follows:
I suggest you to know about both fprintf and disp functions, but for now if you know any one of them you can manage to solve this problem. Checkout the explanation of the program below the program section if you didn't understand the program.
Program
%Ask the user to input the weight in Kilograms kilos = input('Enter the weight in Kilograms: '); %convert kilos to pounds using conversion factor pounds = 0.453592*kilos; fprintf('Weight of %f kilos is %f pounds\n',kilos,pounds)
I have named this file as kilos_to_pounds.m for using this is Matlab command line interface.
Explanation
The above code is simple if you know the basics of Matlab. In the above code we are taking the input of the user asking to Enter the weight in kilograms. Then we convert the kilos to pounds using the conversion factor 1 pound = 0.453592 kilos. Finally we print the output in a very user friendly way so that the user will understand it easily.
Input/Output
Trial run for kilos_to_pounds.m file |
I have tried to explain the code so that it will be easy even for the beginners to understand. If you have any doubt or didn't understand any part of the program then please do contact me from here: contact me, or comment below. This will not only help you but also help others.
Comment you trial run of kilos_to_pounds in the comment below.
Keywords: matlab, pounds, kilos, weight, conversion