In this program inches is converted into yard, feet and inches. Even though "n" is in yard and divided by 12 inches, giving output similar to the manual arithmetic but there is no logic because yard is divided by inches. Please explain the reason.
Manual arithmetic
n = 67/36 = 1.86 yard(36 inches = 1 yard)
Converting remainder 0.86 yard into feet & inches
feet = 36 x 0.86 inches (converting 0.86 yard into inches. 36 inches = 1 yard)
= 30.96 inches
= 2.58 feet
= 2feet 7inches
using System;
public class ConvertInches
{
public static void Main()
{
int inches = 67;
YardsFeetInches(inches);
Console.ReadKey();
}
public static void YardsFeetInches(int number)
{
int yard, n, feet, inches;
yard = number / 36;
n = number % 36; //n - is in yard, 36 inches = 1 yard
feet = n / 12; //n is not converted into inches before divided by 12 (12 inches = 1 feet)
inches = n % 12; //n is not converted into inches before applying remainder operator (12 inches = 1 feet)
Console.WriteLine("{0} yard {1} feet {2} inches", yard, feet, inches);
}
}
//1 yard 2 feet 7 inches
Loading
VulpesPosted May 20, 2012, 10:59 AM
You then divide this by 36 to get the number of complete yards.
The remainder of the 'yard' operation is then divided by 12 to get the number of complete feet (0, 1, or 2).
The remainder of the 'feet' operation then represents the number of inches left over (from 0 to 11).
SenthilkumarPosted May 21, 2012, 12:01 AM
% ---> will do the modular operation
Posted May 20, 2012, 11:07 AM